2013-10-21 129 views
8

打开一个Excel文件,我试图打开从SharePoint使用VBA的Excel文件。因为我每次运行宏时我所寻找的文件可能都不同,所以我希望能够查看SharePoint文件夹并选择我需要的文件。当我想寻找一个网络驱动器上的文件,但是当我更换与SharePoint的地址,我得到“:找不到路径运行时错误76”从SharePoint网站

下面的代码工作正常。

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喜欢吗?

+0

'ChDir()'和'GetOpenFilename'不能在http上工作,但你可以尝试使用sharepoint“webdav”路径而不是http路由。 –

+0

您需要使用WebDAV地址来链接文件; Excel会将其视为网络位置。请参阅下面的答案,了解为您解析URL到WebDAV地址的功能。 – Shrout1

回答

6

试试这个代码从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参考必须启用。此外,您的网站可能会使用反斜杠,我会使用正斜杠。

-1

尝试这样:

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

它为我工作。

1

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

4

我变换网址使用下面的函数我创建了一个WebDAV的地址。此功能还可毫无损坏地返回常规系统路径和UNC路径。

将此函数添加到您的VBA项目的模块中,然后在文件对话框命令后面并在使用文件对话框选择的路径之前输入MyNewPathString = 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 

此功能将检查你的文件的路径是一个URL,如果它是安全的(HTTPS)或不安全(HTTP)。如果它是一个URL,那么它将构建相应的WebDAV字符串,以便您可以直接链接到SharePoint中的目标文件。

每次打开文件时,用户都可能会被提示输入凭据,特别是如果他们与您的SharePoint场不在同一个域中。

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

+0

此外,此答案在Windows XP中不起作用,因为Windows XP不支持WebDAV。所以赢7和更新。 – Shrout1

0

请注意,在你最初的代码一个错字

MyNewPathString = ParseResource(myFileDialogStringVariable) 

被替换
MyNewPathString = Parse_Resource(myFileDialogStringVariable) 

The下划线不见了。