Selecting printer via code.

F

FatMan

Hi all:
Can anyone please tell me the VBA code needed to select a printer different
than the default printer to print a report. I will also need to know how to
change the selected print back to the default.

In other words what I am looking to do is.....

Print report A to the default printer
Change the selected printer to printer X
Print report B to printer X
Change the selected printer back to the default printer

Any help is greatly appreciated.

Thanks,
FatMan
 
R

Ralph

I used WMI Service to accomplish this using a form. On the form I have a list
box named lstPrinters and a command button named cmdSetDefPrinter.

The form load populates the listbox (lstPrinters) with installed printers:

Private Sub Form_Load()
Dim objWMIService
Dim colInstalledPrinters
Dim objPrinter
Dim lstCtrl As ListBox
Dim i As Integer
Set lstCtrl = Me.lstPrinters

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\.\root\cimv2")

Set colInstalledPrinters = objWMIService.ExecQuery _
("Select * from Win32_Printer")

For Each objPrinter In colInstalledPrinters
lstCtrl.AddItem (objPrinter.Name)
Next

Set objWMIService = Nothing
Set colInstalledPrinters = Nothing
Set lstCtrl = Nothing
End Sub


The command button(cmdSetDefPrinter) changes the default printer:

Private Sub cmdSetDefPrinter_Click()
Dim objWMIService
Dim colInstalledPrinters
Dim objPrinter

Set lstCtrl = Me.lstPrinters
strPrinter = lstCtrl.Value

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colInstalledPrinters = objWMIService.ExecQuery _
("Select * from Win32_Printer")

For Each objPrinter In colInstalledPrinters
If objPrinter.Name = strPrinter Then
objPrinter.SetDefaultPrinter
End If
Next
End Sub


There may be an easier way but I could not find one.
 
T

Tony Toews [MVP]

FatMan said:
In other words what I am looking to do is.....

Print report A to the default printer
Change the selected printer to printer X
Print report B to printer X
Change the selected printer back to the default printer

You can change the default printer using updates to the INI file and
some sample code.
See DefaultPRT.ZIP at http://www.mcwtech.com/downloads.htm. This MDB
has a combo box where you select the printer.

Finally you can use DevPrtMode however this requires extensive VBA
knowledge and is not for the faint of heart. Also this won't work in
MDEs as you must open reports in design mode although you can use
Automation to open a reports MDB and manipulate it there.

Tony
--
Tony Toews, Microsoft Access MVP
Please respond only in the newsgroups so that others can
read the entire thread of messages.
Microsoft Access Links, Hints, Tips & Accounting Systems at
http://www.granite.ab.ca/accsmstr.htm
Tony's Microsoft Access Blog - http://msmvps.com/blogs/access/
 
S

Stuart McCall

FatMan said:
Hi all:
Can anyone please tell me the VBA code needed to select a printer
different
than the default printer to print a report. I will also need to know how
to
change the selected print back to the default.

In other words what I am looking to do is.....

Print report A to the default printer
Change the selected printer to printer X
Print report B to printer X
Change the selected printer back to the default printer

Any help is greatly appreciated.

Thanks,
FatMan

You should find that this:

http://www.smccall.demon.co.uk/Reports.htm#SetDevice

does exactly what you want.
 
A

Allen Browne

Tony, it looks like MCW have changed that page to:
http://www.mcwtech.com/downloads.aspx

But the link to 'Chapter 10 (Controlling Your Printer)' is broken. Returns:
The resource you are looking for has been removed,
had its name changed, or is temporarily unavailable.
 
A

Allen Browne

If this is Access 2002, 2003, or 2007, you can just set the Printer object
before you OpenReport, e.g.:
Set Printer = Printers(1)

Use the DeviceName if you want to use the name (more reliable than the
number.)

To set it back to the default:
Set Printer = Nothing

If you want the user to be able to select a printer (which may not be on
your computer at design time), and have the report remember to use that
printer, see:
Printer Selection Utility - Users assign printers to reports
at:
http://allenbrowne.com/AppPrintMgt.html
 
A

Albert D. Kallal

You don't mention what version\ of ms-access.

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 your print....

Swtich back.

Set Application.Printer = Application.Printers(strDefaultPrinter)

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

http://www.members.shaw.ca/AlbertKallal/msaccess/msaccess.html
 
A

Allen Browne

Thanks for that Alex. I've added a direct link to that file on my Links
page. If anyone knows how to link to the explanatory text (Chapter 10 of
Litwin/Getz' book), I'd love to fix that that too.
 
F

FatMan

Tony, Alex and Allen:
Thank you for your help. I think I will go with the form you have suggested
and place it in the reports "on open" event. That way the user can select
the printer before printing.

Once again thanks for the help!
FatMan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top