Declare the items as Object.
You also have your For Each loop out of order.
Function GetCPUID() As String
Dim cimv2 As Object
Dim PInfo As Object
Dim PItem As Object
Dim PubStrComputer As String
PubStrComputer = "."
Set cimv2 = GetObject("winmgmts:\\" & PubStrComputer & "\root\cimv2")
Set PInfo = cimv2.ExecQuery("Select * From Win32_Processor")
For Each PItem In PInfo
GetCPUID = PItem.ProcessorID
Next PItem
End Function
BTW, you do know that identical CPUs will return the same ID? Not the same as a serial number. For example:
Item Value
------ -----
Processor Name Intel(R) Core(TM) i7-7700 CPU @ 3.60GHz
Code Name Kaby Lake
Info Intel64 Family 6 Model 158 Stepping 9
Maker GenuineIntel
ID BFEBFBFF000906E9
Max CPU Speed 3.6 GHz
Physical CPUs 1
Physical Cores 4
Logical Cores 8
Address Width 64
HyperThreading Enabled
VM Firmware Disabled
Socket U3E1
Update: Using Powershell I looked at the properties for the CPU_Object. It includes a Serial number but when checked on my PC I got this:
PS> $CPU_Object.serialnumber
To Be Filled By O.E.M.
HTH
0
solved how to locked excel by cpuid in vba [closed]