Thursday, December 10, 2009

How to get a list of printers in Microsoft Access

Someone ask me for help with a bit of VBA code. He wanted to get a list of all the printers so he could then set the default one. His method used Windows API calls to enumerate the printers. I then introduced to him the MS Access object named "Printers" and then removed 30 lines of code he had been working on.

Here is how I solved his problem:

Dim p As Printer
For Each p In Printers
Debug.Print p.DeviceName
Next

The Printers object is a collection of Printer objects. To set the printer that Access uses, you can set the Application.Printer object to the desired printer.

'You can use the DeviceName string as a key
'to look up a specific printer.
Set Application.Printer = Printers("Adobe PDF")

No comments:

Post a Comment