0

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

Mathieu Guindon
  • 69,817
  • 8
  • 107
  • 235
Ger Cas
  • 2,188
  • 2
  • 18
  • 45
  • Try going step by step and assign a variable to each position of your chained call to the value. This should point you in the right direction. – L8n Sep 23 '19 at 16:45
  • Is the query async? Are you trying to access the object before it is set? – L8n Sep 23 '19 at 16:45
  • @L8n Hi, I'm not sure if it is async, but when try to access the object is already filled with the values like shown in image. The first Item's value is zero. I've already tried assign only `a = WMIvalues.Items` but I get error too. – Ger Cas Sep 23 '19 at 16:49
  • 2
    Also, `_` (underscore) is a bit problematic in VBA as is is also used when writing methods that belong to an interface. Or for Wrapping lines in the VBE. – L8n Sep 23 '19 at 16:50
  • `a = WMIvalues.Items` will fail if `a` is still declared as a String, declare each variable as the expected type. Also, if possible use early-binding instead of late binding: https://stackoverflow.com/a/50583968/10223558 – L8n Sep 23 '19 at 16:57
  • 1
    From what i can guess you will have to use the "Microsoft WMI Scripting V1.2 Library" – L8n Sep 23 '19 at 16:59
  • Thanks for your help. I added the reference from your suggestion "Microsoft WMI Scripting V1.2 Library" but the issue was the same. I tried @TimWilliams suggestion and it works. Thank you. – Ger Cas Sep 23 '19 at 17:24
  • 2
    There's no need to add a reference since your code uses late binding. – Tim Williams Sep 23 '19 at 17:42

1 Answers1

1

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/

Tim Williams
  • 154,628
  • 8
  • 97
  • 125
  • Hi. Excellent Tim. Thanks for your help and link shared. It seems to work for me either. Only one last question, since `WMIvalues` always I run have 2 Items and the values shown for Item1 are always zero `0`. How to run `For Each o in WMIvalue` to show only the values of Item2 and forward if there are more than 2 Items within `WMIvalue`? I don't want to print the first values if they are zero. Thanks – Ger Cas Sep 23 '19 at 17:23
  • You can write an `If` to check the values and take the appropriate action. – Tim Williams Sep 23 '19 at 17:42
  • Yes, I can do `If o.BytesReceivedPersec <> 0 Then Debug.Print....` but how can I set the `If` to detect if only the values of `first Item` are zero? – Ger Cas Sep 23 '19 at 17:50
  • Thank you! I see, checking if `i>0`. I was wondering something more similar to my original question but maybe is not possible. Something like don't print `if WMIvalues.Item(i).Properties_.Item(j).Value=0`. – Ger Cas Sep 24 '19 at 21:16
  • There doesn't seem to be a way of indexing directly into `WMIvalues` – Tim Williams Sep 24 '19 at 21:29