Search code examples
windowsvbaexcelwmiwmi-query

Can you return the Volume GUID of a disk with VBA?


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?


Solution

  • 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