Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

URGENT -- Printer Object doesn't return correct port?

Status
Not open for further replies.

Becca999

Programmer
Oct 14, 2003
23
US
Hello,

I need to get the port for a particular printer. If I loop throughthe printers collection comparing device names to my printer name then try to capture the port, it gives me something like nec1 instead of the real port. Does anyone know why or a way to get the port?

Thanks in advance!!
 

Ports can have names. So you may be actually getting the correct information and not realizing it, but without a look at your code we can't be sure.

Becca999, have you read FAQ222-2244 yet?

Good Luck

 
Becca999 - did you get anywhere with this? I have got the same problem which is causing an (intermittent) problem on a user site. I am comparing the Port property of the Printer object with the Port property of all the members of the Printers collection and it doesn't match thanks to fact one has port of HPLJ3100 (Printer object) while the 'matching' entry in the Printers collection has 'Ne00:'.

Could this be specific to Windows XP?

vb5prgrmr - can you enlarge? My code is
Code:
  For Each DefaultPrinter In Printers
    If DefaultPrinter.DeviceName = Printer.DeviceName And _
       DefaultPrinter.Port = Printer.Port And _
       DefaultPrinter.DriverName = Printer.DriverName Then
      Exit For
    End If
  Next
 
I found that just using whatever the Port property said worked.

From printing within VB, just change set the Printer object to the selected printer (just remember to change it back to their default). I ended up storing the index number from the printer object as a global and used that (which works unless they install a printer while the app is running)!
 
Thanks for reply Becca999

I think our problems are slightly different - I am finding that the Port property of the Printer object differs from the Port property of the corresponding element of the Printers collection. I am looping through the latter and comparing with the former but they do not match.

 
This problem has got weirder. I can no longer create the mis-match. Previously the Printer object .Port property returned 'HPLJ3100' while Printers collection returned 'ne00:'). Now (one or two reboots later) they both return 'ne00:' so I get a match. Any suggestions?
 
the ne00 is the network's port name while the HPL3100 is the local port name. Maybe if you explained what you need the port names for?
 
Thanks for further input.

I am using the port name simply to establish when I have found the printer in the printers collection that matches the current default printer so that, I can store details of that printer and make it the default again later (see code above).

Your explanation has started me thinking though. Although the main issue I am trying to address is on a client site, I did manage to get the same problem on my own PC - perhaps the server was unavailable at one point and hence reverted to local port name or something. The printer in question was attached to my own PC.
 
The current default printer is the printer in the Printer object before you do anything to the Printer object (Start up) I store the index in a global variable. I have to loop through the printers connection in my app because we have to print different reports, etc to different locations all the time plus the printer could be local or network while the printer name is identified by a central parameter table. Hope this helps!


Dim TestPrt As Printer
Dim strMsg As String
Dim intPrtIndex As Integer
Dim strPrtName As String

intPrtIndex = 0
gintHotSheetPrinter = 9999
gintKanbanPrinterIndex = 9999
gintRecTktWhitePrinterIndex = 9999
gintRecTktPinkPrinterIndex = 9999


'** Establish printer indexes from Printer's Collection
For Each TestPrt In Printers

intPrtIndex = intPrtIndex + 1
'** Default Printer
If UCase(TestPrt.DeviceName) = UCase(Printer.DeviceName) Then
gintDefaultPrinterIndex = intPrtIndex - 1
End If
'** Report A Printer
If UCase(TestPrt.DeviceName) = UCase(gstrReportAPrinter) & "" Then
gintReportAPrinter = intPrtIndex - 1
ElseIf InStr(1, UCase(gstrReportAPrinter), UCase(gstrComputerName)) Then
'** Is the printer local to that pc? Strip off the workstation ID & Compare
strPrtName = Right(gstrReportAPrinter, ((Len(gstrReportAPrinter)) - (InStr(3, gstrReportAPrinter, "\"))))
If UCase(TestPrt.DeviceName) = UCase(strPrtName) & "" Then
gintReportAPrinter = intPrtIndex - 1
End If
End If
'** ReportB Printer
If UCase(TestPrt.DeviceName) = UCase(gstrReportBPrinter) & "" Then
gintReportBPrinterIndex = intPrtIndex - 1
gstrReportBPrinter = TestPrt.DeviceName & "," & Printer.Port & "," & TestPrt.DriverName
ElseIf InStr(1, UCase(gstrReportBPrinter), UCase(gstrComputerName)) Then
'** Is the printer local to that pc? Strip off the workstation ID & Compare
strPrtName = Right(gstrReportBPrinter, ((Len(gstrReportBPrinter)) - (InStr(3, gstrReportBPrinter, "\"))))
If UCase(TestPrt.DeviceName) = UCase(strPrtName) & "" Then
gintReportBPrinterIndex = intPrtIndex - 1
gstrReportBPrinter = TestPrt.DeviceName & "," & Printer.Port & "," & TestPrt.DriverName
End If
End If

Next

strMsg = ""
If gintReportAPrinter = 9999 Then
'** Didn't find errors printer or not listed in table -> Set Systems default printer
gintReportAPrinter = gintDefaultPrinterIndex
strMsg = vbNewLine & "The ReportA Printer could not be found. "
End If

If gintReportBPrinterIndex = 9999 Then
'** Didn't find errors printer or not listed in table -> Set Systems default printer
gintReportBPrinterIndex = gintDefaultPrinterIndex
gstrReportBPrinter = strPerDefExt
'** Notify User & record in log file
strMsg = strMsg & vbNewLine & "The ReportB Printer could not be found. "
End If

If strMsg <> "" Then
strMsg = strMsg & vbNewLine & _
"Application will use workstation's default printer instead." & _
vbNewLine & String(80, "-") & vbNewLine & strUserInfo
MsgBox strMsg, vbInformation + vbOKOnly + vbApplicationModal, "FYI"
End If






To switch printers in code:
Set Printer = Printers(gintReportAPrinter)
Printer.Print TXT
Printer.EndDoc
Set Printer = Printers(gintDefaultPrinterIndex)




 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top