【问题标题】:Open an Excel file from SharePoint site从 SharePoint 网站打开 Excel 文件
【发布时间】:2013-10-21 21:57:22
【问题描述】:

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

【问题讨论】:

  • ChDir() 和 GetOpenFilename 不能通过 http 工作,但您可以尝试使用共享点“webdav”路径而不是 http 路由。
  • 需要使用WebDAV地址链接文件; Excel 会将其视为网络位置。请参阅下面的答案,了解为您将 URL 解析为 WebDAV 地址的函数。

标签: vba sharepoint excel excel-2010


【解决方案1】:

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

【讨论】:

  • 当我运行此代码时,在.Show 行它要求我“打开文件”(打开窗口出现以选择要打开的文件)。问题可能出在我的链接中吗?
  • @Flaw98 这段代码的重点是打开一个文件对话框(手动选择一个文件)。如果您尝试在不使用文件对话框的情况下打开特定文件,则需要使用不同的方法。
  • 好吧,我无法访问我电脑上的共享点位置,我想直接从共享点地址打开文件,这可能吗?
  • 经验教训:此代码可以选择文件,没有文件对话框,您需要webdav 解决方案,请参阅。
【解决方案2】:

我使用我创建的以下函数将 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 网站上对此进行过测试,但我相信它会起作用。

【讨论】:

  • 此外,此答案在 Windows XP 中不起作用,因为 Windows XP 不支持 WebDAV。所以仅限 Win 7 及更高版本。
  • 我仍然得到 runtimeerror 52;目录(共享点)的文件名或文件号错误。我的文件夹是\\group.sharepoint.com@ssl\sites\ArbeitsVZ\Freigegebene Dokumente。也许与 Freigegebene Dokumente 中的空间有关?
  • @Timo 很有可能。另请注意,这个答案现在已经很老了,很难说 webdav 的默认配置是什么了。如果服务被禁用,这可能是身份验证/访问被拒绝错误的结果,但这是代表我的猜测。尝试引用您的路径,但要真正检查该服务是否可用。错误消息可能与实际的潜在问题不匹配...
【解决方案3】:

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

【讨论】:

    【解决方案4】:

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

    Excel VBA Up-/Download from multiple SharePoint folders

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

    【讨论】:

    • 您应该在此处粘贴您自己的回复,因为这里有更多访问者并删除该帖子。
    【解决方案5】:

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

    MyNewPathString = ParseResource(myFileDialogStringVariable)
    

    应该替换为

    MyNewPathString = Parse_Resource(myFileDialogStringVariable)
    

    下划线不见了。

    【讨论】:

    • 感谢您提及这一点!请记住将 cmets 保留为 cmets 而不是答案 :) 我已在原始回复中进行了更正。
    【解决方案6】:

    虽然这可能不适用于 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
    

    【讨论】:

    • 我仍然得到runtimeerror 52; filename or filenumber wrong 和dir(sharepoint)。
    • 我的网址是https://group.sharepoint.com/sites/ArbeitsVZ/Freigegebene Dokumente
    【解决方案7】:

    试试这样的:

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

    它对我有用。

    【讨论】:

    • 欢迎来到 SO。我从未听说过shell 的功能。 it worked for me的解释没有用,我不是一个吃饱了就开心的动物。我是灵魂伴侣,除了it worked 之外还需要一些信息。说实话很抱歉。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-14
    • 2017-07-26
    • 2018-12-04
    • 2022-08-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多