Environment information using WMI classes

Environment information is very valuable to the VBA programmer. The Win32_Environment WMI class returns information about the local computer environment.

From MSDN:

The Win32_Environment WMI class represents an environment or system environment setting on a Windows computer system. Querying this class returns environment variables found in:

HKLM\System\CurrentControlSet\Control\Sessionmanager\Environment

and
HKEY_USERS\\Environment

Sample usage

Function GetEnvironmentInfo()

Dim objWMI As Object
Dim environ As Object
Dim envir As Object

  Set objWMI = GetWMIService

  Set environ = objWMI.ExecQuery("Select * from Win32_Environment")

  For Each envir In environ
    Debug.Print envir.Caption
    Debug.Print envir.Description
    Debug.Print envir.InstallDate
    Debug.Print envir.Name
    Debug.Print envir.Status
    Debug.Print envir.SystemVariable
    Debug.Print envir.UserName
    Debug.Print envir.VariableValue
  Next envir

End Function

Function GetWMIService() As Object
' http://msdn.microsoft.com/en-us/library/aa394586(VS.85).aspx
Dim strComputer As String

  strComputer = "."

  Set GetWMIService = GetObject("winmgmts:" _
                              & "{impersonationLevel=impersonate}!\\" _
                              & strComputer & "\root\cimv2")
End Function

Site last updated July 26, 2010 @ 8:14 pm