Excelpromax123
Board Regular
- Joined
- Sep 2, 2021
- Messages
- 172
- Office Version
- 2010
- Platform
- Windows
Hello everyone. I have used the code below to get the hard drive code, but when I plug in the USB or connect another printer, the code gets the code from the USB or the printer. So is there a way to only get the hard drive code, even if an external device is plugged into the computer or not? Thank you
VBA Code:
Function GetDDSerialNumber(Optional sHost As String = ".") As String
On Error Resume Next
Dim oWMI As Object
Dim oDDs As Object
Dim oDD As Object
Set oWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & sHost & "\root\cimv2")
Set oDDs = oWMI.ExecQuery("SELECT DeviceID, SerialNumber FROM Win32_DiskDrive")
For Each oDD In oDDs
GetDDSerialNumber = GetDDSerialNumber & Trim(oDD.SerialNumber)
Next
End Function