473,500 Members | 1,943 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Change printers with VBA

PCB
Hi all!

I have a subroutine that prints reports on a specific printer. The code
works fine as long as all the printers available to the computer are on
the network. However, if the computer also has a local printer, the
report is prints locally. What I'm I doing wrong! The complete
subroutine follows:

Private Sub PrintReport(strReportName As String)
On Error GoTo PrintReport_Err

'Declare variables
Dim Error_Message As Variant
Dim objPrinter As Object
Dim prtDefaultPrinter As Printer

'Declare constants
Const NO_PRINTER_INSTALLED As Long = 2205
Const REPORT_PRINTER As String = "\\NCOTTA14\P5-19884"

'Get default printer
prtDefaultPrinter = Application.Printer

'Open the report in design view
DoCmd.OpenReport strReportName, acViewDesign

'Change printer from default to "REPORT_PRINTER"
For Each objPrinter In Application.Printers
If objPrinter.DeviceName = REPORT_PRINTER Then
Set Application.Printer = objPrinter
End If
Next

'Change "REPORT_PRINTER" properties
With Reports(strReportName).Printer
.Copies = 1
.Orientation = acPRORLandscape
.Duplex = acPRDPVertical
.PaperBin = acPRBNAuto
.PaperSize = acPRPS11x17
.PrintQuality = acPRPQHigh
End With

'Print and close report
DoCmd.PrintOut
DoCmd.Close acReport, strReportName, acSaveNo

'Restore default printer
Application.Printer = prtDefaultPrinter

PrintReport_End:
Exit Sub

PrintReport_Err:
Select Case Err.Number
Case NO_PRINTER_INSTALLED
Error_Message = MsgBox(e_Message_001, vbOKOnly + vbInformation)
Resume PrintReport_End
Case Else
MsgBox Prompt:=Err.Description, Buttons:=vbCritical & vbOKOnly, _
Title:="Error Number " & Err.Number & " Occurred"
Resume PrintReport_End
End Select
End Sub

Jun 9 '06 #1
1 18249
Peter, I don't understand why the report is being opened in design view, and
printed with the PrintOut method (which loses you the chance to use a
WhereCondition.)

Provided you set the printer *before* you OpenReport, it should work fine.
Try:
Dim prtDefaultPrinter As Printer
Set prtDefaultPrinter = Application.Printer
Application.Printer = Application.Printers(REPORT_PRINTER)
DoCmd.OpenReport strReportName, acViewNormal
Application.Printer = prtDefaultPrinter

If you did want to set the orientation etc, you could OpenReport in
acViewPreview, and set those.

For a more comprehensive example that allows you to remember a default
printer for each report, see:
http://allenbrowne.com/AppPrintMgt.html

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"PCB" <pe***********@sympatico.ca> wrote in message
news:11*********************@i40g2000cwc.googlegro ups.com...
Hi all!

I have a subroutine that prints reports on a specific printer. The code
works fine as long as all the printers available to the computer are on
the network. However, if the computer also has a local printer, the
report is prints locally. What I'm I doing wrong! The complete
subroutine follows:

Private Sub PrintReport(strReportName As String)
On Error GoTo PrintReport_Err

'Declare variables
Dim Error_Message As Variant
Dim objPrinter As Object
Dim prtDefaultPrinter As Printer

'Declare constants
Const NO_PRINTER_INSTALLED As Long = 2205
Const REPORT_PRINTER As String = "\\NCOTTA14\P5-19884"

'Get default printer
prtDefaultPrinter = Application.Printer

'Open the report in design view
DoCmd.OpenReport strReportName, acViewDesign

'Change printer from default to "REPORT_PRINTER"
For Each objPrinter In Application.Printers
If objPrinter.DeviceName = REPORT_PRINTER Then
Set Application.Printer = objPrinter
End If
Next

'Change "REPORT_PRINTER" properties
With Reports(strReportName).Printer
.Copies = 1
.Orientation = acPRORLandscape
.Duplex = acPRDPVertical
.PaperBin = acPRBNAuto
.PaperSize = acPRPS11x17
.PrintQuality = acPRPQHigh
End With

'Print and close report
DoCmd.PrintOut
DoCmd.Close acReport, strReportName, acSaveNo

'Restore default printer
Application.Printer = prtDefaultPrinter

PrintReport_End:
Exit Sub

PrintReport_Err:
Select Case Err.Number
Case NO_PRINTER_INSTALLED
Error_Message = MsgBox(e_Message_001, vbOKOnly + vbInformation)
Resume PrintReport_End
Case Else
MsgBox Prompt:=Err.Description, Buttons:=vbCritical & vbOKOnly, _
Title:="Error Number " & Err.Number & " Occurred"
Resume PrintReport_End
End Select
End Sub

Jun 10 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
5883
by: Aaron_TekRecycle.com | last post by:
Someone must have done this before?!? I have VBS code that will Enumerate all the Printers in the AD and Add the Printer Connection to the client... I'm just not a web developer so I need some...
1
1471
by: Vanessa | last post by:
Hi, I'm trying to loop through all the printers in my computer system using WMI. However, I found out that it doesn't really get the correct number of printers in my system. I have 16...
7
14689
by: Don Seckler | last post by:
I have a database that is going to be used to do billing on orders. I sent up a field for each record that is called Billed and is a yes/no field. I want to do a query that will pull all the...
4
50185
by: Nothing | last post by:
I have a form that I send to a printer. The workstation that is using the DB has more then one printer defined. The default is NOT the printer that I want to use for output from the report/form. ...
5
7087
by: Hank | last post by:
My Access 2000 code has been running for several years in our main plant. Now we need to install it, as a stand-along application, at remote sites, some of which are out of state. My problem is...
7
9042
by: trint | last post by:
How can I add all the network printers to a combobox? Thanks, Trint
0
1566
by: Ravigwipro | last post by:
Hi, I m able to get the printers object to know what are the printers had been installed and all. here my requirement is i have to write a data from VB to MS Word. for the page setup i have to...
25
30772
by: Peng Yu | last post by:
Hi, It is possible to change the length of "\t" to a number other than 8. std::cout << "\t"; Thanks, Peng
4
5229
by: Frank Rizzo | last post by:
I basically need a list of printers that's returned by the Find Printers dialog ( http://www.sqleffects.com/mystuff/findPrinters.png ). I've tried the path of DirectoryEntry entry = new...
0
7018
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7182
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6906
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7397
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5490
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4923
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4611
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3106
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1430
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.