如何在VBA中列出可用的网络驱动器(由于使用Sharepoint)?
我们公司最近安装了SharePoint,这在运行Word VBA加载项时有时会产生不利影响。我们目前正在使用Office 2013,但Excel似乎基本上没有受到影响 对我们来说,一件有效的事情是使用如何在VBA中列出可用的网络驱动器(由于使用Sharepoint)?,vba,sharepoint,ms-word,Vba,Sharepoint,Ms Word,我们公司最近安装了SharePoint,这在运行Word VBA加载项时有时会产生不利影响。我们目前正在使用Office 2013,但Excel似乎基本上没有受到影响 对我们来说,一件有效的事情是使用net use,将SharePoint区域映射到“遗留”驱动器号 net use z: https://sharepoint.con/departmentalStuff/etc/... 由于它持续存在于多个用户登录中,并且在取消或更改之前一直有效,net use对用户来说并不难 当然,addin用
net use
,将SharePoint区域映射到“遗留”驱动器号
net use z: https://sharepoint.con/departmentalStuff/etc/...
由于它持续存在于多个用户登录中,并且在取消或更改之前一直有效,net use
对用户来说并不难
当然,addin用户会间接使用VBA提供的各种固定对话框,例如Application.FileDialog(msoFileDialogFilePicker)
,在其不同的Sharepoint和非Sharepoint目录之间导航,并选择文件。这些对话框显示由net use
分配的旧驱动器号的文件。到目前为止还不错
但是,从Sharepoint源中选取的罐装VBA对话框将不会返回旧文件名,而是返回与下游代码不兼容的UNC文件名。我的黑客解决方法是使用以下函数(改编自SO上的代码)简单地转换文件名:
除了WScript.Network
,还有更好的方法来确定所有驱动器映射吗?使用Word加载项时,解决方案是否特定于Sharepoint
Public Function toLegacyName(uncName As String) As String
uncName = Replace(uncName, "https://", "//")
uncName = Replace(uncName, "/", "\")
If Left(uncName, 2) <> "\\" Then
toLegacyName = uncName
Exit Function
End If
Dim network As Object: Set network = CreateObject("WScript.Network")
Dim netDrives As Object: Set netDrives = network.EnumNetworkDrives
Dim drives As String: drives = "Network drive Mappings:" & Chr(13)
Dim myLet As String
Dim myUnc As String
Dim i As Long
' loop through all network drives
For i = 0 To netDrives.Count - 1 Step 2
drives = drives & "Drive " & netDrives(i) & " = " & netDrives(i + 1) & Chr(13)
myLet = netDrives.Item(i)
myUnc = netDrives.Item(i + 1)
myUnc = Replace(myUnc, "@SSL", "") ' installation-dependent ?
' hopefully we find it here
If InStr(LCase(uncName), LCase(myUnc)) Then
toLegacyName = myLet & Right(uncName, Len(uncName) - Len(myUnc))
Exit Function
End If
Next i
' drive not mapped or something
MsgBox drives, vbOKOnly, "Error with UNC file name " & uncName
toLegacyName = ""
End Function
net use x: https://sharepoint.con/departmentalStuff/Marketing/...
net use y: https://sharepoint.con/departmentalStuff/Research/...