Thisworkbook.path - 网络位置

时间:2014-07-28 15:35:23

标签: excel excel-vba vba

在电子表格存储在Dropbox上时使用此代码返回:

Sub output_set()
Dim s As String 
s = ThisWorkbook.Path
outputstr = s & "\output\"
end sub 

结果

\\psf\Dropbox\test_folder\output\

我更愿意返回分配给此位置的驱动器号。

W:\test_folder\output\

如何修改我的宏?

1 个答案:

答案 0 :(得分:2)

Sub Tester()

    Debug.Print MappedDrivePath("\\psf\Dropbox\test_folder\output\")

End Sub


Function UncToMappedDrive(uncPath) As String
    Dim rv As String
    Dim objWMI As Object
    Dim disks As Object
    Dim disk As Object

    rv = ""

    Set objWMI = GetObject("winmgmts:" & _
           "{impersonationLevel=impersonate}!\\.\root\cimv2")
    Set disks = objWMI.ExecQuery("Select * from Win32_MappedLogicalDisk")

    For Each disk In disks
        If uncPath Like disk.ProviderName & "*" Then
            rv = Replace(uncPath, disk.ProviderName, disk.Name)
            Exit For
        End If
    Next disk

    UncToMappedDrive = rv
End Function