John Smith said:
How can we specifiy a printer in VBA code?.. The problem is that
when I print some report, I want them to print to PDF File instead of
the Default Printer
That depends on what version of Access you have installed, and what PDF
"printer". I'm using Access 2002, which has a Printer object and
Printers collection that can be used as shown in the code below. (Note:
this code is expecting the find the CutePDF printer, but failing that
will attempt to print to any printer with "PDF" in its name.)
'----- start of code (watch out for line wraps) -----
Private Sub cmdPDF_Click()
Dim prt As Printer
Dim intPrt As Integer
If IsNull(Me.txtFromDate) Then
MsgBox "Please enter the From date first!"
Me.txtFromDate.SetFocus
Exit Sub
End If
With Application.Printers
' Look first for the CutePDF printer.
For intPrt = 0 To (.Count - 1)
If .Item(intPrt).DeviceName = "CutePDF Printer" Then
Set prt = .Item(intPrt)
Exit For
End If
Next intPrt
' Did we find the CutePDF printer?
If prt Is Nothing Then
' No. Search for any PDF printer.
For intPrt = 0 To (.Count - 1)
If InStr(.Item(intPrt).DeviceName, "PDF") > 0 Then
Set prt = .Item(intPrt)
Exit For
End If
Next intPrt
End If
End With
' Did we find a PDF printer?
If prt Is Nothing Then
MsgBox "Sorry, I can't find a PDF printer installed on your
system!", _
vbExclamation, "No PDF Printer"
Else
Set Application.Printer = prt
DoCmd.OpenReport conTimeSheetReport, acViewNormal
Set Application.Printer = Nothing
Set prt = Nothing
End If
End Sub
'----- end of code -----