3

I came across a another post VBA Open a USB device using it's unique id in wmi that was unanswered where the poster showed a way to get a USB device UID via VBA and WMI but I am not sure how to retreive the VolumeGUIDs out of WMI with VBA.

I would like to get the DeviceID and then return the VolumeGUIDs if possible for a particular device if it matches DeviceID.

This post How to get the volume GUID seems to suggest it is possible but it is for C++

Am I just querying the wrong WMI class?

Community
  • 1
  • 1
Matt
  • 483
  • 1
  • 4
  • 22

1 Answers1

3

I used to have VBS to do this, you can reduce and modify it to be used in VBA.

strComputer = "." 
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")

Volume
MountPoint


Sub Volume()
    Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Volume") ' Windows XP and earlier:  not available.
    wscript.echo "[ Win32_Volume ] - " & colItems.Count & " items"
    For Each objItem in colItems
        ShowT "Access", objItem.Access
        ShowT "Automount", objItem.Automount
        ShowT "Availability", objItem.Availability
        ShowT "BlockSize", objItem.BlockSize
        ShowT "Capacity", objItem.Capacity
        ShowT "Caption", objItem.Caption
        ShowT "Compressed", objItem.Compressed
        ShowT "Description", objItem.Description
        ShowT "DeviceID", objItem.DeviceID
        ShowT "DirtyBitSet", objItem.DirtyBitSet
        ShowT "DriveLetter", objItem.DriveLetter
        ShowT "DriveType", objItem.DriveType
        ShowT "FileSystem", objItem.FileSystem
        ShowT "FreeSpace", objItem.FreeSpace
        ShowT "IndexingEnabled", objItem.IndexingEnabled
        ShowT "Label", objItem.Label
        ShowT "MaximumFileNameLength", objItem.MaximumFileNameLength
        ShowT "Name", objItem.Name
        ShowT "NumberOfBlocks", objItem.NumberOfBlocks
        ShowT "PNPDeviceID", objItem.PNPDeviceID
        ShowT "Purpose", objItem.Purpose
        ShowT "Status", objItem.Status
        ShowT "StatusInfo", objItem.StatusInfo
        ShowT "SerialNumber", objItem.SerialNumber
        ShowT "SupportsDiskQuotas", objItem.SupportsDiskQuotas
        ShowT "SupportsFileBasedCompression", objItem.SupportsFileBasedCompression
        wscript.echo "-----"
    Next
    wscript.echo vbCrlf & "====================" & vbCrlf
End Sub

Sub MountPoint()
    Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_MountPoint")
    wscript.echo "[ Win32_MountPoint ] - " & colItems.Count & " items"
    For Each objItem in colItems
        ShowT "Directory", objItem.Directory
        ShowT "Volume", objItem.Volume
        wscript.echo "-----"
    Next
    wscript.echo vbCrlf & "====================" & vbCrlf
End Sub

Sub ShowT(s, obj)
    If Len(obj) > 0 Then Wscript.Echo vbTab & s & ": " & obj
End Sub
PatricK
  • 6,375
  • 1
  • 21
  • 25
  • I believe I should be able to convert this thank you I will give it a shot! – Matt Jan 19 '16 at 06:39
  • So i was able to get it working but is there any way to get the VolumeGUIDs from the DeviceID in Win32-PnPEntity I posted the question at http://stackoverflow.com/questions/34873073/retrieve-volumeguids-via-win32-pnpentity-using-vba – Matt Jan 19 '16 at 10:03