PDA

View Full Version : [SOLVED:] WSH to List All Printers



BrI
03-21-2019, 09:43 AM
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


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

BrI
03-21-2019, 11:10 AM
Decided to go with method below, there are a variety of methods listed a following link if anyone interested: https://social.technet.microsoft.com/Forums/scriptcenter/en-US/0db5ce88-af14-4333-84cd-cef9a05aee2c/find-printername-and-printerport-on-vbscript?forum=ITCG


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

snb
03-22-2019, 07:17 AM
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

BrI
03-22-2019, 09:35 AM
Thanks SNB,

Example result of your code:
"\\appprirpspr01\MTN-1-A-3-CHQ on 10.250.8.17"

If I use

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?