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

Excelpromax123

Board Regular
Joined
Sep 2, 2021
Messages
167
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

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
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,215,321
Messages
6,124,239
Members
449,149
Latest member
mwdbActuary

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