+ Reply to Thread
Results 1 to 3 of 3

Get WMI Win32 classes info with VBA

Hybrid View

  1. #1
    Registered User
    Join Date
    02-21-2010
    Location
    Italy
    MS-Off Ver
    Excel 2013
    Posts
    77

    Question Get WMI Win32 classes info with VBA

    Dear All,
    such a long time from my last post but hopefully I'm here again to find some help.
    This time I'm trying to get system info via WMI with Excel VBA and I wanted to ask some advices.

    My goal is to list all values for each property of a give WMI.Win32_xxxx class.
    My question is about how to get all the keys for all properties listed without actually write in my code all properties' names.

    I explain myself better, I have the code below:

    Function GetOSInfo(strClassName) As String
     
        Dim objWMIService As Object
        Dim colItems As Object
        Dim objItem As Object
        Dim strComputer As String
        Dim strOSName As String
        Dim strWQL As String
     
        strComputer = "."
     
        strWQL = "SELECT * FROM " & strClassName
     
        Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\CIMV2")
     
        Set colItems = objWMIService.ExecQuery(strWQL)
    
     
        For Each objItem In colItems
            ' Get the Value of the Properties
            ' START OF CODE PART I WANT TO ENHANCE
            Debug.Print objItem.Name
            Debug.Print objItem.BootDevice
            Debug.Print objItem.BuildNumber
            Debug.Print objItem.Caption
            Debug.Print objItem.CodeSet
            Debug.Print objItem.CountryCode
            Debug.Print objItem.CreationClassName
            Debug.Print objItem.CSDVersion
            Debug.Print objItem.CSName
            Debug.Print objItem.CurrentTimeZone
            Debug.Print objItem.DataExecutionPrevention_32BitApplications
            Debug.Print objItem.DataExecutionPrevention_Available
            Debug.Print objItem.DataExecutionPrevention_Drivers
            Debug.Print objItem.DataExecutionPrevention_SupportPolicy
            Debug.Print objItem.Debug
            Debug.Print objItem.Description
            Debug.Print objItem.Distributed
            Debug.Print objItem.EncryptionLevel
            Debug.Print objItem.ForegroundApplicationBoost
            Debug.Print objItem.FreePhysicalMemory
            'Debug.Print objItem.FreeSpaceInPaginFiles
            Debug.Print objItem.FreeVirtualMemory
            Debug.Print objItem.InstallDate
            Debug.Print objItem.LargeSystemCache
            Debug.Print objItem.LastBootUpTime
            Debug.Print objItem.LocalDateTime
            Debug.Print objItem.Locale
            Debug.Print objItem.Manufacturer
            Debug.Print objItem.MaxNumberOfProcesses
    
            [...]
    
            ' END OF CODE PART I WANT TO ENHANCE
    
        Next
     
     
        Set objItem = Nothing
        Set colItems = Nothing
        Set objWMIService = Nothing
     
    End Function
    
    
    Sub GetData()
    GetOSInfo("Win32_OperatingSystem")
    End Sub
    It's a simple code in which I call a function passing the class I want to list the properties' values from.
    Since the class Win32_OperatingSystem contains 63 properties, I'm wondering if is there a way to loop through all of them in order to get those data without writing all the names of the properties.
    I mean, something like:


    For Each objItem In colItems
        For i=1 to colItems.Item 1.Properties_.Count
            Debug.Print colItems.Item[1]. Properties_[i].Value
        Next
    Next
    Unluckily this code doesn't work because I'm not referencing correctly the structure of the class.

    Any help would be highly appreciated.

    regards,
    PP
    Last edited by fredpox; 05-31-2013 at 01:09 PM. Reason: SOLVED

  2. #2
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Get WMI Win32 classes info with VBA

    The swbemobject has a properties_ property
    
    Set objClass = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2:" & strClassName
    
    For Each objClassProperty in objClass.Properties_
        Debug.print objClassProperty.Name
    Next
    there is also a Methods_ property :-)
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  3. #3
    Registered User
    Join Date
    02-21-2010
    Location
    Italy
    MS-Off Ver
    Excel 2013
    Posts
    77

    Re: Get WMI Win32 classes info with VBA

    Dear Joseph, thanks a lot for your valuable help! That did the trick!

    PS just a small correction in your first string (added the bracket at the end of the line)
    Set objClass = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2:" & strClassName)
    Thanks again

    Best regards,
    Paolo

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1