Function setprinter(requiredprinter As String, preview As Boolean) As Boolean
'requiredprinter is the name of the printer you want to use
'preview is a setting to determine whether to preview or not
'this sub selects the printer and returns control to the calling app, ready for the print to start
Dim ptrname As String
Dim ptr As Printer
Dim useprinter As Printer
'try and find this printer
For Each ptr In Application.Printers
If ptr.DeviceName = requiredprinter Then
Set useprinter = ptr
GoTo gotit
End If
Next
noprinter:
MsgBox ("The expected printer: " & requiredprinter & " was not found. The " & rep & " will be set to print to your default printer. ")
Exit Function
gotit:
On Error GoTo badPrinter
Set Application.Printer = useprinter
'i added this bit as a generic bit of information about the print.
'this can be commented out if not required
If preview Then
MsgBox ("The printer selected for this report has been set to printer " & ptrname & ". " & vbCrLf & vbCrLf & _
"The print preview will now open. You can select a different printer if you require. ")
Else
MsgBox ("The printer selected for this report has been set to printer " & ptrname & ". " & vbCrLf & vbCrLf & _
"The print will now start automatically on this printer. ")
End If
Exit Function
badPrinter:
MsgBox ("There was an error setting the printer to " & ptrname & vbCrLf & _
"The report will print on your default printer. " & vbCrLf & vbCrLf & _
"Error: " & Err & " Desc: " & Err.Description)
End Function
Function clearprinter() As Boolean
'reassign the printer to the default
Set Application.Printer = Nothing
End Function