-
WSH to List All Printers
I'm using WSH in excel VBA to list Printer Name, Driver and Port of the default printer in code below.
Wondering if this could be modified (using WSH) to loop through all printers and get info for each.
I have found a method that works at link below, but was hoping to modify my method to keep it simple. http://www.cpearson.com/Excel/GetPrinters.aspx
Code:
Sub GetDefault()
Set WshShell = CreateObject("WScript.Shell")
defaultprn = WshShell.RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows\Device")
Debug.Print "Default Printer: " & Split(defaultprn, ",")(0)
Debug.Print "Driver: " & Split(defaultprn, ",")(1)
Debug.Print "Port: " & Split(defaultprn, ",")(2)
End Sub
-
Decided to go with method below, there are a variety of methods listed a following link if anyone interested: https://social.technet.microsoft.com...ipt?forum=ITCG
Code:
Sub PrinterList()
Const HKEY_CURRENT_USER = &H80000001
Const REG_SZ = 1
strComputer = "."
Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
strComputer & "\root\default:StdRegProv")
strKeyPath = "Software\Microsoft\Windows NT\CurrentVersion\Devices"
objReg.EnumValues HKEY_CURRENT_USER, strKeyPath, _
arrValueNames, arrValueTypes
For i = 0 To UBound(arrValueNames)
If arrValueTypes(i) = REG_SZ Then
objReg.GetStringValue HKEY_CURRENT_USER, _
strKeyPath, arrValueNames(i), strValue
'WScript.Echo arrValueNames(i) & " on " & Split(strValue, ",")(1)
Debug.Print arrValueNames(i) & " on " & Split(strValue, ",")(1)
End If
Next
End Sub
-
Code:
Sub M_snb_printers()
MsgBox ActivePrinter
With CreateObject("Wscript.network")
For j = 0 To .EnumPrinterConnections.Count - 1 Step 2
c00 = c00 & vbLf & .EnumPrinterConnections(j + 1) & " on " & .EnumPrinterConnections(j)
Next
End With
MsgBox c00
End Sub
-
Thanks SNB,
Example result of your code:
"\\appprirpspr01\MTN-1-A-3-CHQ on 10.250.8.17"
If I use
Code:
Debug.Print arrValueNames(i) & " on " & Split(strValue, ",")(1) & Split(strValue, ",")(0)
in my code I get: "\\appprirpspr01\MTN-1-A-3-CHQ on Ne06:winspool"
I'm OK with my method but wondering if yours can be modified to also return the Ne port and driver?