Excel VBA + List all mapped network drives and network shortcuts/locations

和自甴很熟 提交于 2019-12-11 18:39:33

问题


I'm creating a dropdown list on open that populates the user's mapped network drives (drive letter) and any network locations (folder shortcut). Basically what you would see in the Network Locations section of My Computer/This PC.

Using VBA I know you can sift through mapped drives using the filescripting object but I've been unsuccessful getting network locations.

Trying the Wscript.Network object with the Object.enumNetworkDrives appeared to work fine at work yet doesn't appear to bring in the network locations while at home working remote.

Is there a different, better and/or more consistent method (with a good VBA example) to getting all the mapped drives and network locations using VBA?


回答1:


I suggest you have a look at WMI. Here's a short example to get you started:

Sub ListDrives()
    WQL = "Select * From Win32_LogicalDisk"
    Set SrvEx = GetObject("winmgmts:root/CIMV2")
    Set WMIObj = SrvEx.ExecQuery(WQL)
    For Each WMIObjEx In WMIObj
        Debug.Print WMIObjEx.Path_.RelPath
    Next
End Sub


来源:https://stackoverflow.com/questions/54853165/excel-vba-list-all-mapped-network-drives-and-network-shortcuts-locations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!