Link to home
Start Free TrialLog in
Avatar of coolchillen
coolchillen

asked on

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?

Thanks,
Matt
Avatar of Leo Eikelman
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 = 10.0.0.70 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.


Leo
Avatar of coolchillen

ASKER

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.

Matt
   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
    Next
    Set PrinterList = Nothing
    Set WMIService = Nothing

wow, i love the enthusiasm Jack...man, 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,
matt
ASKER CERTIFIED SOLUTION
Avatar of JackOfPH
JackOfPH
Flag of Philippines image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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!
:) oh ya, i've been here, me and msdn have sort of a "love hate" relationship.

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