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)