从SharePoint网站打开Excel文件

我正尝试使用VBA从SharePoint打开Excel文件。 由于我正在寻找的文件可能会不同,每次运行macros时,我想能够查看SharePoint文件夹并select我需要的文件。

当我想在networking驱动器上查找文件时,下面的代码工作正常,但是当我用SharePoint地址replace时,出现“运行时错误76:path未find”。

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资源pipe理器中时,我没有访问SharePoint文件夹的问题,所以我知道path是正确的。

为什么VBA不喜欢它?

试试这段代码从SharePoint站点中select一个文件:

 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引用。 此外,您的网站可能会使用反斜杠,我的使用正斜杠。

我使用我创build的以下函数将URL转换为WebDAV地址。 此函数还可以毫无损坏地返回常规的系统path和UNCpath。

通过将其添加到VBA项目中的模块中,并在文件对话框命令之后并使用文件对话框select的path之前inputMyNewPathString = ParseResource(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 

这个函数将检查你的文件path是否是一个URL,是否安全(HTTPS)或不安全(HTTP)。 如果它是一个URL,那么它将构build相应的WebDAVstring,以便您可以直接链接到SharePoint中的目标文件。

每次打开文件时,都可能会提示用户input凭据,特别是如果它们不与SharePoint场处于同一个域中。

请注意:我没有testing过这个与http网站,但我相信它会工作。

从你的脚本不要使用http://sharepoint/my/file作为path,而是\\sharepoint\my\file ,然后应该工作。 它适用于我在C#中完成的程序。

您可以使用我的方法将SharePoint文件夹映射为networking驱动器。 那么你可以继续进行,因为你到目前为止。

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

然后,您还可以使用Dir或File System Object浏览文件

请注意,您的初始代码中存在拼写错误

 MyNewPathString = ParseResource(myFileDialogStringVariable) 

应该换成

 MyNewPathString = Parse_Resource(myFileDialogStringVariable) 

下划线不见了

尝试这样的事情:

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

它为我工作。