I found this by Rory Archibald.
Sub PrintersAndPorts()
'This works with Windows 2000 and up
Dim Arr As Variant
Dim Device As Variant
Dim Devices As Variant
Dim msg As String
Dim RegObj As Object
Dim RegValue As String
Const HKEY_CURRENT_USER = &H80000001
Set RegObj = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
RegObj.enumvalues HKEY_CURRENT_USER, "Software\Microsoft\Windows NT\CurrentVersion\Devices", Devices, Arr
For Each Device In Devices
RegObj.getstringvalue HKEY_CURRENT_USER, "Software\Microsoft\Windows NT\CurrentVersion\Devices", Device, RegValue
msg = msg & Device & " on " & Split(RegValue, ",")(1) & vbCrLf
Next
MsgBox msg, vbInformation, "Printers and Ports"
End Sub
If you want the result in Column A, starting at cell A2, as Leith Ross did with this.
Sub PrintersAndPorts()
'This works with Windows 2000 and up
Dim Arr As Variant
Dim Device As Variant
Dim Devices As Variant
Dim Msg As String
Dim RegObj As Object
Dim RegValue As String
Dim R As Long
Dim Rng As Range
Dim Wks As Worksheet
Const HKEY_CURRENT_USER = &H80000001
Set Wks = Worksheets("Sheet1")
Set Rng = Wks.Range("A2")
Set RegObj = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
RegObj.enumvalues HKEY_CURRENT_USER, "Software\Microsoft\Windows NT\CurrentVersion\Devices", Devices, Arr
For Each Device In Devices
RegObj.getstringvalue HKEY_CURRENT_USER, "Software\Microsoft\Windows NT\CurrentVersion\Devices", Device, RegValue
Rng.Offset(R, 0) = Device & " on " & Split(RegValue, ",")(1)
R = R + 1
Next
End Sub
Bookmarks