Code to get a fixed hard drive code, not changed when USB is plugged in

Excelpromax123

Board Regular
Joined
Sep 2, 2021
Messages
172
Office Version
  1. 2010
Platform
  1. 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
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Yes, you can modify the code to only get the hard drive code, even if an external device is plugged into the computer or not. Here's an example of how you can modify the code to achieve this:

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, MediaType, SerialNumber FROM Win32_DiskDrive") For Each oDD In oDDs If oDD.MediaType = "Fixed hard disk media" Then GetDDSerialNumber = Trim(oDD.SerialNumber) Exit For End If Next End Function


In the code above, the MediaType property of the Win32_DiskDrive class is checked, and the code only gets the serial number of the fixed hard disk media.
 
Upvote 1

Forum statistics

Threads
1,224,820
Messages
6,181,157
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top