Vba 从SharePoint网站打开Excel文件

Vba 从SharePoint网站打开Excel文件,vba,sharepoint,excel,excel-2010,Vba,Sharepoint,Excel,Excel 2010,我正在尝试使用VBA从SharePoint打开Excel文件。因为每次运行宏时我要查找的文件可能不同,所以我希望能够查看SharePoint文件夹并选择所需的文件 当我想在网络驱动器上查找文件时,下面的代码可以正常工作,但是当我将其替换为SharePoint地址时,会出现运行时错误76:找不到路径 Sub Update_monthly_summary() Dim SummaryWB As Workbook Dim SummaryFileName As Variant ChDir "http

我正在尝试使用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不喜欢它?

试试这样的方法:

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

这对我很有用。

请尝试以下代码从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脚本运行时引用。此外,您的站点可能使用反斜杠,而我的站点使用正斜杠。

您的脚本不使用反斜杠http://sharepoint/my/file 作为道路而不是
\\sharepoint\my\文件,然后是应该工作的。它适用于用C编写的程序。

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

通过将此函数添加到VBA项目的模块中,并在文件对话框命令之后和使用文件对话框选择的路径之前输入MyNewPathString=Parse_ResourcemyFileDialogStringVariable来调用此函数。然后在使用目标文件位置时引用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站点测试过这一点,但是我相信它会起作用。

您可以使用我的方法将SharePoint文件夹映射为网络驱动器。然后你就可以像现在这样继续了


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

请注意,您的初始代码中有一个输入错误

MyNewPathString = ParseResource(myFileDialogStringVariable)
应替换为

MyNewPathString = Parse_Resource(myFileDialogStringVariable)

下划线丢失。

虽然这可能不完全适用于OP的“需要打开文件”对话框,但这就是我如何硬编码打开通过SharePoint/Team存储的工作簿的方式,该工作簿与标题匹配,可能也是许多人在这里寻找的内容

通过点击复制链接并在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

ChDir和GetOpenFilename不能通过http工作,但您可以尝试使用sharepoint webdav路径而不是http路由;Excel会将其视为网络位置。有关将URL解析为WebDAV地址的函数,请参见下面的我的答案。此外,此答案在Windows XP中不起作用,因为Windows XP不支持WebDAV。因此,仅限Win 7及更新版本。感谢您的提醒!请记住将注释保留为注释而不是答案:我已在原始响应中更正了它。当我运行此代码时,在第行。显示它要求我打开文件打开窗口,以便选择要打开的文件。问题可能出在我的链接中?@failk98此代码的要点是打开一个文件对话框以手动选择一个文件。如果您试图在不使用文件对话框的情况下打开特定文件,则需要使用不同的方法。嗯,我无法访问我电脑上的sharepoint位置,我想直接从sharepoint地址打开文件,这是否可行?