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