Code Samples for Businesses, Schools & Developers

First Published 2 Dec 2023


An earlier article Get Default Printer showed how to get a list of all installed physical and virtual printers using code and to identify the default printer.

It is also very simple to set the default printer in Access using code. Place this code in a standard module.

CODE:

Function SetDefaultPrinter()

'Adapted from https://learn.microsoft.com/en-us/office/vba/api/access.application.printer

      Dim prtDefault As printer

      'select a printer from the printers collection as listed using the GetDefaultPrinter function
      'The collection numbering is zero based. Change the number as appropriate
      Set Application.printer = Application.Printers(0)
      ' . . . or use the printer name
      'Set Application.printer = Application.Printers("YourPrinterNameHere")

      Set prtDefault = Application.printer

      With prtDefault
            MsgBox "Device name: " & .DeviceName & vbCrLf & _
                  "Driver name: " & .DriverName & vbCrLf & _
                  "Port: " & .Port, vbInformation, "Default Printer"
      End With

End Function



Alternatively, you may wish to change the printer temporarily and then revert to the original default printer.
For example, you may wish to use a specialized label printer for a particular report.

To do so, use code similar to this:

CODE:

Function ChangeDefaultPrinter()

      Dim ptr As printer

      'Get current default printer
      Set ptr = Application.printer

      'modify it to a specified printer
      Set Application.printer = Application.Printers("YourPrinterNameHere")

      'add code here to print whatever you need to do . . .

      'optional - restore original default printer
      Set Application.printer = ptr

End Function




Download:

Both code snippets are included in the attached zip file together with the related code to get a list of installed printers

         modDefaultPrinter      (bas - zipped)

After unzipping, you can then import the modDefaultPrinter.bas file directly into the Visual Basic Editor



Related Articles

      Get Default Printer

      Count Jobs in Print Queue



Feedback

Please use the contact form below to let me know whether you found this article interesting/useful or if you have any questions/comments.

Please also consider making a donation towards the costs of maintaining this website. Thank you



Colin Riddington           Mendip Data Systems                 Last Updated 2 Dec 2023



Return to Code Samples Page




Return to Top