Macro to Print Reports

dpmaki

Board Regular
Joined
Sep 12, 2011
Messages
165
I found this snippet of code on here that should print a report that you specify out of an Access DB.

Code:
Private Sub PrintUS028()
Dim defPrinter As String, NewPrinter As Printer
'Get the default printer name
defPrinter = Application.Printer.DeviceName
'Create a new printer object
Set NewPrinter = Application.Printers("MTR1-DL5210N-005") 'use your printer name here
'Set the default printer to the new printer
Set Application.Printer = NewPrinter
'Open the report
DoCmd.OpenReport "US028-FINAL REPORT", acViewNormal  'use your actual report name here
'Print the report (the next line seems to print the report and the form so I removed it, if your report doesn't print then remove the apostrophe on the next line)
'DoCmd.PrintOut
'Close the report
DoCmd.Close acReport, "US028-FINAL REPORT", acSaveNo  'report name again!
'Reset the printer back to the default printer
Set NewPrinter = Application.Printers(defPrinter)
Set Application.Printer = NewPrinter
End Sub

When I run it - it fails on the line: Set NewPrinter = Application.Printers("MTR1-DL5210N-005") 'use your printer name here
Giving me this error: Invalid procedure call or argument

I assume that I'm to use the name of the printer as it appears on the network, but maybe I'm incorrect?
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
If you already have a default printer defined on your system there is no need to select one in code -- the default printer will be the target.

Denis
 
Upvote 0

Forum statistics

Threads
1,221,537
Messages
6,160,402
Members
451,644
Latest member
hglymph

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top