Getting Printer Port Information

Posted on 2006-04-26
Last Modified: 2007-11-27
I have an application that will run on about 25 different computers at my office and it needs to identify a couple particular printers on its own.  The problem is, i dont want to use the printer name as an identifyer (Printers(x).DeviceName) because some computers have had there default print name changed to something a little more friendly (ex. "Deskjet 5600" changed to "Color Printer").  So i thought i would use the "port name" because i thought that would be consistant (they are all network printers on TCP/IP printer ports ex. "IP_10.0.0.70").  The port names as they are listed in the printer properties dialog are nicely named but when i call "Printers(x).Port", it returns ne00, ne01 ect. which of course is not consistant from one workstation to the next.

Does anyone know how to get the "friendly" port names, as seen in the printers properties dialog? or know any other way to identify a particular printer without knowing it's name?

Question by:coolchillen
    LVL 8

    Expert Comment

    by:Leo Eikelman
    Try this code.  I don't know if this will work as I used it to compare printer names but it might for IPs.

    Dim prt As Printer

    For Each prt In Printers

    If prt.Port = Then 'You would compare to IP address instead

    Set Printer = prt

    strDeviceName = prt.DeviceName
    strDriverName = prt.DriverName
    strPort = prt.Port

    Exit For

    End If
    Next prt

    Set prt = Nothing

    Report.SelectPrinter strDriverName, strDeviceName, strPort

    Report.PrintOut False

    strDeviceName should hold the friendly name of the printer.


    Author Comment

    Thanks Leo,

    Unfortunatly prt.Port returns something like LPT1 or ne01 not the "IP_10.0.0.70" port name found in the printer properties dialog on the port tab that i thought i could use as an identifier.

    The problem is that depending on what order different printers were setup on an individual workstation, the printer i want could be ne01 on one system and ne04 on another...they all have the "IP_10.0.0.70" port checked in the printer properties dialog, i just can seem to extract that from a Printer object.

    LVL 15

    Expert Comment

       Dim RemoteComputer as String
        Dim WMIService as Object
        Dim PrinterList as Object
        Dim PrinterInstance as Object

        RemoteComputer = "."   'Replace the . with the name of the remote PC
        Set WMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & RemoteComputer & "\root\cimv2")
        Set PrinterList = WMIService.ExecQuery("Select * from Win32_Printer")
        For Each PrinterInstance In PrinterList
            MsgBox PrinterInstance.Sharename
        Set PrinterList = Nothing
        Set WMIService = Nothing


    Author Comment

    wow, i love the enthusiasm, you triggered some delusions of granger in my head...but it will have to wait for version 2 of may app...and until the WMI reference book i just ordered comes in.

    I was looking for a more "no networking" based solution...but your way seems way cooler.  I didn't find alot of good reference to the WMI so i ordered a book (i'm kind of a noob).  I see that your example uses the current user authentication credentials to access the remote machine...when i put my current login/password into the remote system i get an "automation error".  If you got another tip to get me a step further, that would be great, otherwise i'm going to have to hold off on this route right now (even though it seems totally cool) because of time constraints but thanks again. (and thanks for motivating me to get some good reference material...finaly picked up a book on the win32 api as well)

    Again, the challenger here is to identify a common network printer on multiple systems that have the same network printer configured with a different name on each system.

    thanks Jack,
    LVL 15

    Accepted Solution

    Dim devlist as object
    Dim dev as object
    Set devlist = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery("Select * from Win32_Printer")
    For Each dev In devlist
        MsgBox "Monitor: " & dev.ShareName
    devlist = Nothing

    I try this without a network connected in my P.C. and it returns the same.

    Author Comment

    sweat, that did the trick...except i used the .PortName property instead of the .ShareName property.  Interestingly, "devlist = nothing" returns "Object doesn't support this property or method" every time.

    Also, i am unfamilior with this WMI, if you have any recommended references, i would appreciate the clue.

    Thanks Jack!
    LVL 15

    Expert Comment


    Author Comment

    :) oh ya, i've been here, me and msdn have sort of a "love hate" relationship.

    Really, this and msdn is all you need?  How come 95% of the reference material i find about this "WMI" looks alot like C to me?

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
    Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now