Search code examples
excelvbawmiwql

How to get the values from Items within Object?


I have the following code and query to get the Sent/Received Bytes from Wlan connection. I want to get the values for each Properties' Item but I get Generic failure when trying like this:

a = WMIvalues.Item(1).Properties_.Item(1).Value

How would be the correct way to do it?

 Sub Test()
    Dim WMIvalues As Object
    Dim sWQL      As String


    sWQL = "Select BytesReceivedPersec,BytesSentPersec,BytesTotalPersec  from  Win32_PerfRawData_Tcpip_NetworkInterface"

    Set WMIvalues = GetObject("winmgmts:root/CIMV2").ExecQuery(sWQL)

    a = WMIvalues.Item(1).Properties_.Item(1).Value

End Sub

enter image description here


Solution

  • This works for me:

    Dim WMIvalues As Object
    Dim sWQL      As String
    Dim o As Object, i As Long
    
    sWQL = "Select BytesReceivedPersec,BytesSentPersec,BytesTotalPersec  from  " _
            "Win32_PerfRawData_Tcpip_NetworkInterface"
    
    Set WMIvalues = GetObject("winmgmts:root/CIMV2").ExecQuery(sWQL)
    
    i = 0
    For Each o In WMIvalues
        i = i + 1 'increment item counter variable
        Debug.Print o.BytesReceivedPersec, o.BytesSentPersec, o.BytesSentPersec, o.BytesTotalPersec
    
        'logic here based on i and the o properties...
    Next o
    

    See: https://www.activexperts.com/admin/scripts/wmi/vbscript/0473/