none
Find Printername and PrinterPort on VBScript

    Question

  • Hi

    Im trying to obtain a list of available printers and their addres on the local computer.

    When Excel print a file it doese it in the printer address on Printer port like "\\PrinterServer\PrinterName On Ne003:"

    using VBScript I create a an Excel opbject

     Set oExcel = CreateObject("Excel.Application")
     oExcel.Application.Visible = False
     oExcel.WorkBooks.Open sFile, 3, False

    Then I have to print the file but I only have the Printer Name, the netport depend on the local machine.

     If cDbl(oExcel.Version) < 12 Then
      oExcel.ActiveWorkBook.PrintOut,, 1,, sPrinter, False
     Else
      oExcel.ExecuteExcel4Macro "PRINT(1,,,1,,,,,,,,1,""" & sPrinter & """,,TRUE,,FALSE)"
     End If

    I have find that VisualBasic has a Printers colection and a Printer Object part of the VB.Global

    I can execute this code and obtain a list of available printers asn theyr ports

        Dim l_pr As Printer

        For Each l_pr In Printers
            Debug.Print l_pr.DeviceName & " On " & l_pr.Port
        Next

    Now I need to be able to creat an object in VBscript that can give me the same information, but I haven been able so fare.

    Plase Help

    Thursday, February 3, 2011 8:27 AM

Answers

  • If that registry key contains the values you need you can read the values and construct a printer name in the format you can use in Excel. Here is an example:

    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)
      End If 
    
    Next
    
    

     

    First enumerate the value names and read data for each value. You can use Split to get the port name (Split(strValue, ",")(1))

     

     


    Uros Calakovic
    • Marked as answer by IamMred Saturday, February 19, 2011 12:43 AM
    Sunday, February 13, 2011 1:22 PM
    Moderator

All replies

  • you can use he win32_printer class to query printers.

    http://www.computerperformance.co.uk/vbscript/wmi_printer.htm

    Option Explicit
    Dim objWMIService, objItem, colItems, strComputer, intPrinters
    
    strComputer ="."
    intPrinters = 1
    
    ' --------------------------------------------
    ' Pure WMI Section
    Set objWMIService = GetObject _
    ("winmgmts:\\" & strComputer & "\root\CIMV2")
    Set colItems = objWMIService.ExecQuery _
    ("SELECT * FROM Win32_Printer")
    
    Call Wait() ' Goto Sub Routine at the end
    
    ' On Error Resume Next
    For Each objItem In colItems
    WScript.Echo "Printers on " _
    & objItem.name & ", Printer Number: " & intPrinters & VbCr & _
    "====================================" & VbCr & _
    "Printer: " & objItem.DeviceID & VbCr & _
    "Port Name: " & objItem.PortName & VbCr & _
    
    intPrinters = intPrinters + 1
    Next
    
    sub Wait()
    If strComputer = "." then
    strComputer = "Local Host"
    else strComputer = strComputer
    end if
    
    WScript.Echo "Wait 2 mins for " & strComputer _
    & " to enumerate printers"
    
    End Sub
    


    Thiyagu | MCTS/MCITP - Exchange 2007 | MCSE 2003[Messaging] | http://www.myExchangeWorld.com. This posting is provided "AS IS" with no warranties, and confers no rights.
    Thursday, February 3, 2011 9:33 AM
    Moderator
  • There is also the EnumPrinterConnections that is part of WSH ...

      Set WshNetwork = CreateObject("WScript.Network")
      Set oPrinters = WshNetwork.EnumPrinterConnections
      s = "Network printer mappings:"
      For i = 0 to oPrinters.Count - 1 Step 2
        s = s & vbCRLF & "Port " & oPrinters.Item(i) & " = " & oPrinters.Item(i+1)
      next
      wsh.echo s


    Tom Lavedas
    Thursday, February 3, 2011 9:40 PM
    Moderator
  • I have already try this method ant the port mention here doese not match the port where the printer is located when used by Excel

    On my computer this is the result after running your method

    Network printer mappings:
    Port XPSPort: = THT_50 XPSPort:
    Port FILE: = THT_50 FILE:
    Port nul = RemotePC Image Printer
    Port XPSPort: = Microsoft XPS Document Writer
    Port Microsoft Office Live Meeting 2007 Document Writer Port: = Microsoft Office Live Meeting 2007 Document Writer
    Port CPW2: = CutePDF Writer
    Port PRTBNE7N.corp.company.com = \\BNEPRT03\PRTBNE7N

    and running my rutine on Visual Basic

    THT_50 XPSPort: On Ne00:
    THT_50 FILE: On FILE:
    RemotePC Image Printer On nul
    Microsoft XPS Document Writer On Ne01:
    Microsoft Office Live Meeting 2007 Document Writer On Ne02:
    CutePDF Writer On CPW2:
    \\BNEPRT03\PRTBNE7N On Ne03:

    When I search the Printer settings in the Windows Registry I find this

    [HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Devices]
    "THT_50 XPSPort:"="winspool,Ne00:"
    "THT_50 FILE:"="winspool,FILE:"
    "RemotePC Image Printer"="winspool,nul"
    "Microsoft XPS Document Writer"="winspool,Ne01:"
    "Microsoft Office Live Meeting 2007 Document Writer"="winspool,Ne02:"
    "CutePDF Writer"="winspool,CPW2:"
    "\\\\BNEPRT03\\PRTBNE7N"="winspool,Ne03:"

    any idea how could I get this in VBScript?

    Thursday, February 3, 2011 10:25 PM
  • Thanks for the idea, but this is not working for me

    The list of printer generated by the script you sent on my computer is

    Printer List:
    Printer: THT_50 XPSPort: Port Name: XPSPort:
    Printer: THT_50 FILE: Port Name: FILE:
    Printer: RemotePC Image Printer Port Name: nul
    Printer: Microsoft XPS Document Writer Port Name: XPSPort:
    Printer: Microsoft Office Live Meeting 2007 Document Writer Port Name: Microsoft Office Live Meeting 2007 Document Writer Port:
    Printer: CutePDF Writer Port Name: CPW2:
    Printer: \\BNEPRT03\PRTBNE7NPort Name: PRTBNE7N.corp.company.com

    I find something similar as your sugestion yesterday and after looking inside the object I couden't find any field containing what I was looking for

    I try changing the SQl Script from Win32_Printer to Win32_PrinterDrivers but the result is the same.

    Is this query obtaining a result from the windows registry?
    becaus under [HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Devices] there is the information I'm looking for.

    Thursday, February 3, 2011 10:54 PM
  • If that registry key contains the values you need you can read the values and construct a printer name in the format you can use in Excel. Here is an example:

    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)
      End If 
    
    Next
    
    

     

    First enumerate the value names and read data for each value. You can use Split to get the port name (Split(strValue, ",")(1))

     

     


    Uros Calakovic
    • Marked as answer by IamMred Saturday, February 19, 2011 12:43 AM
    Sunday, February 13, 2011 1:22 PM
    Moderator