Consulting

Results 1 to 4 of 4

Thread: WSH to List All Printers

  1. #1
    VBAX Regular
    Joined
    Apr 2017
    Posts
    66
    Location

    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

    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

  2. #2
    VBAX Regular
    Joined
    Apr 2017
    Posts
    66
    Location
    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

    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

  3. #3
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,635
    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

  4. #4
    VBAX Regular
    Joined
    Apr 2017
    Posts
    66
    Location
    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?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •