VBA – Get Processor Id

I was helping someone out in a forum that was trying to determine the computer’s Processor Id to use it as part of a registration system.

Once again WMI automation can enable us to get this information. Below was the function I put together.

'---------------------------------------------------------------------------------------
' Procedure : GetProcessorId
' Author    : Daniel Pineault, CARDA Consultants Inc.
' Website   : http://www.cardaconsultants.com
' Purpose   : Retrieve the specified PC's Processor(s) Id(s)
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
'             (CC BY-SA 4.0) - https://creativecommons.org/licenses/by-sa/4.0/
' Req'd Refs: Uses Late Binding, so none required
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' sHost     : Name/IP Address of the PC to query assuming you have the rights to do so
'               optional, so by leaving it blank it will query the local computer
'
' Usage:
' ~~~~~~
' ? GetProcessorId
'
' Revision History:
' Rev       Date(yyyy/mm/dd)        Description
' **************************************************************************************
' 1         2012-11-22              Initial Release
' 2         2018-09-03              Added header, updated copyright and error handling
'---------------------------------------------------------------------------------------
Function GetProcessorId(Optional sHost As String = ".") As String
    'Win32_Processor -> https://docs.microsoft.com/en-us/windows/desktop/cimwin32prov/win32-processor
    On Error GoTo Error_Handler
    Dim oWMI                  As Object           'WMI object to query about the PC's OS
    Dim oProcessors           As Object           'Collection of OSs
    Dim oProcessor            As Object           'Individual OS

    Set oWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & sHost & "\root\cimv2")
    Set oProcessors = oWMI.ExecQuery("SELECT ProcessorId FROM Win32_Processor")

    For Each oProcessor In oProcessors
        GetProcessorId = GetProcessorId & oProcessor.ProcessorId & ","
    Next
    If Right(GetProcessorId, 1) = "," Then GetProcessorId = Left(GetProcessorId, Len(GetProcessorId) - 1)

Error_Handler_Exit:
    On Error Resume Next
    If Not oProcessor Is Nothing Then Set oProcessor = Nothing
    If Not oProcessors Is Nothing Then Set oProcessors = Nothing
    If Not oWMI Is Nothing Then Set oWMI = Nothing
    Exit Function

Error_Handler:
    MsgBox "The following error has occurred." & vbCrLf & vbCrLf & _
           "Error Number: " & Err.Number & vbCrLf & _
           "Error Source: GetProcessorId" & vbCrLf & _
           "Error Description: " & Err.Description, _
           vbCritical, "An Error has Occurred!"
    Resume Error_Handler_Exit
End Function

Furthermore, you can easily modify the above to list other properties. Below is a variation that lists all the available processor properties

'---------------------------------------------------------------------------------------
' Procedure : GetProcessorProperties
' Author    : Daniel Pineault, CARDA Consultants Inc.
' Website   : http://www.cardaconsultants.com
' Purpose   : List all the available WMI Processor Processors
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
'             (CC BY-SA 4.0) - https://creativecommons.org/licenses/by-sa/4.0/
' Req'd Refs: Uses Late Binding, so none required
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' sHost     : Name/IP Address of the PC to query assuming you have the rights to do so
'               optional, so by leaving it blank it will query the local computer
'
' Usage:
' ~~~~~~
' ? GetProcessorProperties
'
' Revision History:
' Rev       Date(yyyy/mm/dd)        Description
' **************************************************************************************
' 1         2012-11-22              Initial Release
' 2         2018-09-03              Added header, updated copyright and error handling
'---------------------------------------------------------------------------------------
Function GetProcessorProperties(Optional sHost As String = ".") As String
    'Win32_Processor -> https://docs.microsoft.com/en-us/windows/desktop/cimwin32prov/win32-processor
    On Error GoTo Error_Handler
    Dim oWMI                  As Object           'WMI object to query about the PC's OS
    Dim oProcessors           As Object           'Collection of OSs
    Dim oProcessor            As Object           'Individual OS
    Dim oPrp                  As Object

    Set oWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & sHost & "\root\cimv2")
    Set oProcessors = oWMI.ExecQuery("SELECT ProcessorId FROM Win32_Processor")
    For Each oProcessor In oProcessors
        For Each oPrp In oProcessor.Properties_
            Debug.Print oPrp.Name, oPrp.Value
        Next oPrp
    Next

Error_Handler_Exit:
    On Error Resume Next
    If Not oPrp Is Nothing Then Set oPrp = Nothing
    If Not oProcessor Is Nothing Then Set oProcessor = Nothing
    If Not oProcessors Is Nothing Then Set oProcessors = Nothing
    If Not oWMI Is Nothing Then Set oWMI = Nothing
    Exit Function

Error_Handler:
    MsgBox "The following error has occurred." & vbCrLf & vbCrLf & _
           "Error Number: " & Err.Number & vbCrLf & _
           "Error Source: GetProcessorProperties" & vbCrLf & _
           "Error Description: " & Err.Description, _
           vbCritical, "An Error has Occurred!"
    Resume Error_Handler_Exit
End Function

These functions are not restricted to Access and will work in any VBA compatible application (Access, Excel, Word, …).