![](/img/trans.png)
[英]Excel 2016: open and close an Excel file programmatically from sharepoint
[英]Open an Excel file from SharePoint site
我正在嘗試使用 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 不喜歡它?
嘗試使用以下代碼從 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
參考。 此外,您的網站可能使用反斜杠,我的網站使用正斜杠。
我使用我創建的以下函數將 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 站點測試過這個,但是我相信它會起作用。
從您的腳本中,不要使用http://sharepoint/my/file
作為路徑,而是使用\\\\sharepoint\\my\\file
,然后應該可以工作。 它適用於我在 C# 中完成的程序。
請注意您的初始代碼中有一個錯字
MyNewPathString = ParseResource(myFileDialogStringVariable)
應該替換為
MyNewPathString = Parse_Resource(myFileDialogStringVariable)
下划線不見了。
您可以使用我的方法將 SharePoint 文件夾映射為網絡驅動器。 然后你就可以像之前一樣繼續了。
Excel VBA 從多個 SharePoint 文件夾上傳/下載
然后您還可以使用Dir
或 File System Object 瀏覽文件
雖然這可能無法完全滿足 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
嘗試這樣的事情:
Shell ("C:\Program Files\Internet Explorer\iexplore.exe http://sharepoint/my/file/path")
它對我有用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.