Getting Printer Port Information

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?

Who is Participating?
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.
Leo EikelmanDirector, IT and Business DevelopmentCommented:
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.

coolchillenAuthor Commented:
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.

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

   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

coolchillenAuthor Commented:
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,
coolchillenAuthor Commented:
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!
coolchillenAuthor Commented:
:) 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?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.