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
Thanks!
JS
> 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 -----

Signature
Dirk Goldgar, MS Access MVP
www.datagnostics.com
(please reply to the newsgroup)
Kris Bishop - 13 Apr 2005 21:09 GMT
This is a great piece of code. What can we use in place of the printer
object for those of us who don't have 2002? I'm operating off of Access
2000.