Once again trying to help someone in a thread who needed to set the Application Printer to a specific printer name. Now the issue being, and if you are here you are already ware of the problem, is that when you try and set the application printer it is expecting the printer No, not its name! So what is one to do? Below is a simple function which basically loops through the printer collection and checks for the printer name being called, and once it find that one, it sets the application printer to that printer.
'---------------------------------------------------------------------------------------
' Procedure : SwitchAppPrinter
' Author : Daniel Pineault, CARDA Consultants Inc.
' Website : http://www.cardaconsultants.com
' Purpose : Change the application printer
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
' (CC BY-SA 4.0) - https://creativecommons.org/licenses/by-sa/4.0/
' Req'd Refs: None required
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' sPrinterName : Name of the printer to set the application to use by default
'
' Usage:
' ~~~~~~
' Call SwitchAppPrinter("Snagit 10")
' Call SwitchAppPrinter("Brother MFC-240C")
'
' Revision History:
' Rev Date(yyyy/mm/dd) Description
' **************************************************************************************
' 1 2014-10-23 Initial Release
' 2 2019-03-09 Fixed typo (sPinterName -> sPrinterName)
' Updated error handler and function header
' Made function return boolean if successful or not
' 3 2022-11-15 Fixed Typo in Function name
'---------------------------------------------------------------------------------------
Function SwitchAppPrinter(sPrinterName As String) As Boolean
On Error GoTo Error_Handler
Dim prtAvailPrinters As Printer
For Each prtAvailPrinters In Application.Printers
If prtAvailPrinters.DeviceName = sPrinterName Then
Set Application.Printer = prtAvailPrinters
SwitchAppPrinter= True
Exit For
End If
Next prtAvailPrinters
Error_Handler_Exit:
On Error Resume Next
Exit Function
Error_Handler:
MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Source: SwitchAppPrinter" & vbCrLf & _
"Error Description: " & Err.Description & _
Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
, vbOKOnly + vbCritical, "An Error has Occurred!"
Resume Error_Handler_Exit
End Function
It is a simple workaround to Microsoft not having given us any direct way to set the application printer to a given printer name, which let’s be serious, it the way we need to work! Who knows what number a printer is from one system to another. Setting a printer based on an arbitrary number is moronic! Yes I said it!!!
I should also specify that it would probably be a good idea to initially capture the active default printer and set it back once you are done (as you close your database for instance).
As such, you can determine the active default printer with a couple simple lines of code:
Dim sDefaultPrnt As Printer
Set sDefaultPrnt = Application.Printer
Other Resources:
If you are looking for more printer functions, pere_de_chipstick was kind enough to share some of his code in the UtterAccess Code Archive, see:
Select Printers, Configuration, Paper Sizes And Paper Bins For Reports, Any Version