David
David

Reputation: 115

MS Access - Trying to set printer before printing, but keeps defaulting to Print to PDF

My client wants me to loop through his label printing printers and find one available.

I'm not in his office, so can only default it to my local printer if none of his are found. EXCEPT, it only prints to PDF on my end. I even unchecked the box that said "Let Windows manage your printer selection". It keeps coming up pdf.

Here's the listing of printers on my machine

0 Send To OneNote 2016
1 PDFill PDF&Image Writer
2 Microsoft XPS Document Writer
3 Microsoft Print to PDF
4 Fax
5 Brother HL-2280DW

What am I doing wrong?

Here's my code:

Private Sub cmdPrintWTOutgoingLabels_Click()
Dim LABELprinter As Integer


'check that fields are filled in
If Not IsNumeric(Me.cboOutgoingWT) Then
    MsgBox "Please select a work ticket number first"
    Exit Sub
Else

Dim printerFound As Boolean
Dim numprinters As Integer

printerFound = False
numprinters = Application.Printers.Count - 1


For h = 0 To numprinters


    LABELprinter = Hex(h)


    'if it errors, don't run the code that exits the loop
    On Error GoTo stay_in_loop
    Debug.Print LABELprinter & " " & Application.Printers(h).DeviceName
    If Application.Printer.DeviceName = "ZDesigner GK420d on Ne" & CStr(LABELprinter) & ":" Then

        Set Application.Printer = Application.Printer(h) '"ZDesigner GK420d on Ne" & CStr(LABELprinter) & ":"

        printerFound = True

        Exit For

    End If

stay_in_loop:
Next h
'start error trapping again
On Error GoTo 0
If printerFound = False Then
   Set Application.Printer = Application.Printers(5) 'hard coded to my local printer
            'Sheet5.PrintOut
End If

    DoCmd.OpenReport "WT Outgoing Report", acViewNormal

End If

End Sub

Upvotes: 0

Views: 2800

Answers (1)

Erik A
Erik A

Reputation: 32682

Changing the system default printer for one specific program generally is a terrible user experience. Avoid changing Application.Printer! Instead, specify which printer to use per report instead of printing to the default.

I use something like the following code to print reports to a specific printer:

Dim rptName As String
rptName = "WT Outgoing Report"
DoCmd.OpenReport rptName, acViewPreview
Set Reports(rptName).Printer = Application.Printers(5) 'Or some printer returned by your search code
DoCmd.SelectObject acReport, rptName
DoCmd.PrintOut
DoCmd.Close acReport, rptName

You can also hard-code which printer to print to by opening a report in design view, going to page setup, then page, then change the printer under Printer for MyReportName.

In my actual application, I've also included code to change things like page settings and bin settings.

Upvotes: 1

Related Questions