18

我正在尝试使用 VBA 从 SharePoint 打开一个 Excel 文件。因为每次运行宏时我要查找的文件都可能不同,所以我希望能够查看 SharePoint 文件夹并选择我需要的文件。

当我想在网络驱动器上查找文件时,下面的代码可以正常工作,但是当我用 SharePoint 地址替换它时,我得到“运行时错误 76:找不到路径”。

Sub Update_monthly_summary()

Dim SummaryWB As Workbook
Dim SummaryFileName As Variant

ChDir  "http://sharepoint/my/file/path"
SummaryFileName = Application.GetOpenFilename("Excel-files,*.xls", _
1, "Select monthly summary file", , False)
If SummaryFileName = False Then Exit Sub

Set SummaryWB = Workbooks.Open(SummaryFileName)

End Sub

当我将此地址粘贴到 Windows 资源管理器中时,访问 SharePoint 文件夹没有问题,因此我知道路径是正确的。

为什么 VBA 不喜欢它?

4

7 回答 7

12

尝试使用以下代码从 SharePoint 网站中选择一个文件:

Dim SummaryWB As Workbook
Dim vrtSelectedItem As Variant

With Application.FileDialog(msoFileDialogOpen)
    .InitialFileName = "https://sharepoint.com/team/folder" & "\"
    .AllowMultiSelect = False
    .Show
    For Each vrtSelectedItem In .SelectedItems
        Set SummaryWB = Workbooks.Open(vrtSelectedItem)
    Next
End With

If SummaryWB Is Nothing then Exit Sub

如果我没记错的话,Microsoft Scripting Runtime必须启用引用。此外,您的站点可能使用反斜杠,我的站点使用正斜杠。

于 2013-10-22T04:14:11.133 回答
11

我使用我创建的以下函数将 URL 转换为 WebDAV 地址。此函数还完好无损地返回常规系统路径和 UNC 路径。

通过将其添加到 VBA 项目中的模块并MyNewPathString = Parse_Resource(myFileDialogStringVariable)在文件对话框命令之后和使用文件对话框选择的路径之前输入来调用此函数。然后在使用目标文件位置时引用“MyNewPathString”。

 Public Function Parse_Resource(URL As String)
 'Uncomment the below line to test locally without calling the function & remove argument above
 'Dim URL As String
 Dim SplitURL() As String
 Dim i As Integer
 Dim WebDAVURI As String


 'Check for a double forward slash in the resource path. This will indicate a URL
 If Not InStr(1, URL, "//", vbBinaryCompare) = 0 Then

     'Split the URL into an array so it can be analyzed & reused
     SplitURL = Split(URL, "/", , vbBinaryCompare)

     'URL has been found so prep the WebDAVURI string
     WebDAVURI = "\\"

     'Check if the URL is secure
     If SplitURL(0) = "https:" Then
         'The code iterates through the array excluding unneeded components of the URL
         For i = 0 To UBound(SplitURL)
             If Not SplitURL(i) = "" Then
                 Select Case i
                     Case 0
                         'Do nothing because we do not need the HTTPS element
                     Case 1
                         'Do nothing because this array slot is empty
                     Case 2
                     'This should be the root URL of the site. Add @ssl to the WebDAVURI
                         WebDAVURI = WebDAVURI & SplitURL(i) & "@ssl"
                     Case Else
                         'Append URI components and build string
                         WebDAVURI = WebDAVURI & "\" & SplitURL(i)
                 End Select
             End If
         Next i

     Else
     'URL is not secure
         For i = 0 To UBound(SplitURL)

            'The code iterates through the array excluding unneeded components of the URL
             If Not SplitURL(i) = "" Then
                 Select Case i
                     Case 0
                         'Do nothing because we do not need the HTTPS element
                     Case 1
                         'Do nothing because this array slot is empty
                         Case 2
                     'This should be the root URL of the site. Does not require an additional slash
                         WebDAVURI = WebDAVURI & SplitURL(i)
                     Case Else
                         'Append URI components and build string
                         WebDAVURI = WebDAVURI & "\" & SplitURL(i)
                 End Select
             End If
         Next i
     End If
  'Set the Parse_Resource value to WebDAVURI
  Parse_Resource = WebDAVURI
 Else
 'There was no double forward slash so return system path as is
     Parse_Resource = URL
 End If


 End Function

此函数将检查您的文件路径是否为 URL,以及它是安全的 (HTTPS) 还是不安全的 (HTTP)。如果它是 URL,那么它将构建适当的 WebDAV 字符串,以便您可以直接链接到 SharePoint 中的目标文件。

每次打开文件时,可能都会提示用户输入凭据,尤其是当他们与您的 SharePoint 场不在同一个域时。

请注意:我没有在 http 网站上测试过,但是我相信它会起作用。

于 2014-06-14T15:28:15.160 回答
2

从你的脚本不要http://sharepoint/my/file用作路径,而是 \\sharepoint\my\file应该工作。它适用于我用 C# 完成的程序。

于 2014-03-28T16:26:52.530 回答
1

您可以使用我的方法将 SharePoint 文件夹映射为网络驱动器。然后,您可以像之前那样继续操作。

Excel VBA 从多个 SharePoint 文件夹上传/下载

Dir然后您还可以使用文件系统对象浏览文件

于 2015-06-19T20:35:13.667 回答
1

虽然这可能不适用于 OP 打开文件对话框的需要,但这是我硬编码打开通过 SharePoint/Teams 存储的工作簿的方式,该工作簿与标题相匹配,可能是许多人最终在这里寻找的内容。

通过点击“复制链接”并在“ObjectURL”之后和“baseURL”之前剥离所需的部分来获取 URL。

Sub Test()
Dim URL As String
'Get URL By Coping Link and getting between "ObjectUrl" and "&baseUrl"
'Eg: objectUrl=https%3A%2F%2Fdomain.sharepoint.com%2Fsites%2FName_Teams%2FShared%20Documents%2FGeneral%2FDocuName.xlsx&baseUrl
URL = "https%3A%2F%2Fdomain.sharepoint.com%2Fsites%2FName_Teams%2FShared%20Documents%2FGeneral%2FDocuName.xlsx"
URLDecoded = URLDecode(URL)
'Debug.Print URLDecoded
Set WB = Workbooks.Open(URLDecoded)
End Sub

Public Function URLDecode(StringToDecode As String) As String

Dim TempAns As String
Dim CurChr As Integer

CurChr = 1

Do Until CurChr - 1 = Len(StringToDecode)
  Select Case Mid(StringToDecode, CurChr, 1)
    Case "+"
      TempAns = TempAns & " "
    Case "%"
      TempAns = TempAns & Chr(Val("&h" & _
         Mid(StringToDecode, CurChr + 1, 2)))
       CurChr = CurChr + 2
    Case Else
      TempAns = TempAns & Mid(StringToDecode, CurChr, 1)
  End Select

CurChr = CurChr + 1
Loop

URLDecode = TempAns
End Function
于 2020-02-05T23:54:29.047 回答
1

请注意,您的初始代码中有错字

MyNewPathString = ParseResource(myFileDialogStringVariable)

应该替换为

MyNewPathString = Parse_Resource(myFileDialogStringVariable)

下划线不见了。

于 2017-10-25T07:13:53.180 回答
-1

尝试这样的事情:

Shell ("C:\Program Files\Internet Explorer\iexplore.exe http://sharepoint/my/file/path")

它对我有用。

于 2013-10-21T22:24:36.923 回答