Print To Specific Printer

J

Jeff G

All -

I've looked around for a soluton so that I can have a user print a report to
any printer that is installed on their system. I have most of it figured
out, but I'm running into a problem.

On my form, I have a field called lstPrinters. It is an unbound control
with a Row Source Type of Value List. I have the following code in the Form
Load event...

Private Sub Form_Load()

' Fill the list of printers
For Each objPrinter In Printers
Me.lstPrinters.AddItem objPrinter.DeviceName
Next

End Sub

I do have ojbPrinter declared by putting the following in the declaration...

Private objPrinter As Printer

I have a command button to Print the Report:

Private Sub cmdPrintReport_Click()
On Error Resume Next ' needed in case they click Cancel

Dim rpt As Access.Report
Dim prtr As Access.Printer

Set Application.Printer = Me.lstPrinters.Value
Set prtr = Application.Printer

'Set the default printer's orientation to landscape
prtr.Orientation = acPRORLandscape

'Set the default printer's paper size to legal
prtr.PaperSize = acPRPSLetter

'Print Preview the Alphabetical List of Products Report
DoCmd.OpenReport "rptRevenueActual", acViewNormal
Set rpt = Reports("rptRevenueActual")

DoCmd.Close acReport, "rptRevenueActual" ' close
'Set the Printer property of the report to the
'Application.Printer object
Set rpt.Printer = prtr

End Sub

The problem I'm running into is that I can select any printer other than the
default one, but when I click on the cmdPrintReport button, it only prints
to the default printer.

Is there something missing?

Thanks.

Jeff
 
G

Graham Mandeno

Hi Jeff

The problem is in this line:

Set Application.Printer = Me.lstPrinters.Value

You are trying to set the Application.Printer *object* to a string (the
device name) wheras you should be setting it to a printer object:

Set Application.Printer = Application.Printers(Me.lstPrinters.Value)

Normally this would raise an error, but you are using On Error Resume Next
to ensure that errors are being ignored.

It is much better practice to use an error handler to process all errors and
ignore only those that ought to be ignored (e.g. OpenReport cancelled).

This is very simple to do:

On Error Goto ProcErr:

<body of your procedure here>

ProcEnd:
On Error Resume Next
<clean up code here>
Exit Sub

ProcErr:
If Err <> 2501 then ' no message for error 2501 (DoCmd cancelled)
MsgBox Err.Description, "Error " & Err, vbExclamation
End If
Resume ProcEnd

Also, I notice that you are trying to set the Printer property of the
report, but not until after the report is closed. For a start, this will
fail, but also it is unnecessary because you have already changed the
application's default printer.
 
J

Jeff G

Graham -

Thanks...that did it.

I'm not real familiar with the Printer objects in VBA.

Glad it was something simple.

Jeff

Graham Mandeno said:
Hi Jeff

The problem is in this line:

Set Application.Printer = Me.lstPrinters.Value

You are trying to set the Application.Printer *object* to a string (the
device name) wheras you should be setting it to a printer object:

Set Application.Printer = Application.Printers(Me.lstPrinters.Value)

Normally this would raise an error, but you are using On Error Resume Next
to ensure that errors are being ignored.

It is much better practice to use an error handler to process all errors
and ignore only those that ought to be ignored (e.g. OpenReport
cancelled).

This is very simple to do:

On Error Goto ProcErr:

<body of your procedure here>

ProcEnd:
On Error Resume Next
<clean up code here>
Exit Sub

ProcErr:
If Err <> 2501 then ' no message for error 2501 (DoCmd cancelled)
MsgBox Err.Description, "Error " & Err, vbExclamation
End If
Resume ProcEnd

Also, I notice that you are trying to set the Printer property of the
report, but not until after the report is closed. For a start, this will
fail, but also it is unnecessary because you have already changed the
application's default printer.

--
Good Luck :)

Graham Mandeno [Access MVP]
Auckland, New Zealand

Jeff G said:
All -

I've looked around for a soluton so that I can have a user print a report
to any printer that is installed on their system. I have most of it
figured out, but I'm running into a problem.

On my form, I have a field called lstPrinters. It is an unbound control
with a Row Source Type of Value List. I have the following code in the
Form Load event...

Private Sub Form_Load()

' Fill the list of printers
For Each objPrinter In Printers
Me.lstPrinters.AddItem objPrinter.DeviceName
Next

End Sub

I do have ojbPrinter declared by putting the following in the
declaration...

Private objPrinter As Printer

I have a command button to Print the Report:

Private Sub cmdPrintReport_Click()
On Error Resume Next ' needed in case they click Cancel

Dim rpt As Access.Report
Dim prtr As Access.Printer

Set Application.Printer = Me.lstPrinters.Value
Set prtr = Application.Printer

'Set the default printer's orientation to landscape
prtr.Orientation = acPRORLandscape

'Set the default printer's paper size to legal
prtr.PaperSize = acPRPSLetter

'Print Preview the Alphabetical List of Products Report
DoCmd.OpenReport "rptRevenueActual", acViewNormal
Set rpt = Reports("rptRevenueActual")

DoCmd.Close acReport, "rptRevenueActual" ' close
'Set the Printer property of the report to the
'Application.Printer object
Set rpt.Printer = prtr

End Sub

The problem I'm running into is that I can select any printer other than
the default one, but when I click on the cmdPrintReport button, it only
prints to the default printer.

Is there something missing?

Thanks.

Jeff
 

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