MS Access – Select a Printer

Open the Print Dialog

If all you want is to open the print dialog using code then the following tidbit of code is all you need.

docmd.RunCommand acCmdPrint

Be sure to trap error number 2501 in case the user decides to cancel the action.

Code which printer is used in VBA

On the other hand, if you are looking to hard code which printer is used to print a document then the following is addressed to you.

The following information come from Albert D. Kallal (Access MVP)

In access 2002 and later, there is a built in printer object, and it lets you switch the printer with ease.

You can use:

Set Application.Printer = Application.Printers("HP LaserJet Series II")

So, to save/switch, you can use:

Dim strDefaultPrinter  as string
 
' get current default printer.
strDefaultPrinter = Application.Printer.DeviceName
 
' switch to printer of your choice:
Set Application.Printer = Application.Printers("HP LaserJet Series II")
 
'do whatever....print reports

'Swtich back.
Set Application.Printer = Application.Printers(strDefaultPrinter)

If you are using a earlier versions, then you can use my lightweight printer switch code here:

http://www.members.shaw.ca/AlbertKallal/msaccess/msaccess.htm

So, I do often build a form that displays a list of installed printers, and allow the user to select a printer. The above code example has such a form, but that old example is REALLY only of use for pre-a2002 applications.

So, for special forms, or things like invoice printer, I sill do NOT save which printer with the report (you *can* save the printer in ms-access, but the feature is not much use for users since if they install, or purchase a new printer..then the name changes..and your application will complain). So, while we do switch printers in code..we STILL avoid saving the particular printer to a given report. So, we always still set reports to use the default printer.

Since the margins, and portrait/landscape are saved with the report, then generally, just switching printers should do the trick if we kept the margins fairly large in the reports.”

Allen Browne also has a good utility for this purpose. More details can be found at http://allenbrowne.com/AppPrintMgt.html.

Leave a Reply









Spam protection by WP Captcha-Free