473,788 Members | 2,855 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(str ReportName As String)
On Error GoTo PrintReport_Err

'Declare variables
Dim Error_Message As Variant
Dim objPrinter As Object
Dim prtDefaultPrint er As Printer

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

'Get default printer
prtDefaultPrint er = Application.Pri nter

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

'Change printer from default to "REPORT_PRINTER "
For Each objPrinter In Application.Pri nters
If objPrinter.Devi ceName = REPORT_PRINTER Then
Set Application.Pri nter = objPrinter
End If
Next

'Change "REPORT_PRINTER " properties
With Reports(strRepo rtName).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.Pri nter = prtDefaultPrint er

PrintReport_End :
Exit Sub

PrintReport_Err :
Select Case Err.Number
Case NO_PRINTER_INST ALLED
Error_Message = MsgBox(e_Messag e_001, vbOKOnly + vbInformation)
Resume PrintReport_End
Case Else
MsgBox Prompt:=Err.Des cription, Buttons:=vbCrit ical & vbOKOnly, _
Title:="Error Number " & Err.Number & " Occurred"
Resume PrintReport_End
End Select
End Sub

Jun 9 '06 #1
1 18353
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 prtDefaultPrint er As Printer
Set prtDefaultPrint er = Application.Pri nter
Application.Pri nter = Application.Pri nters(REPORT_PR INTER)
DoCmd.OpenRepor t strReportName, acViewNormal
Application.Pri nter = prtDefaultPrint er

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******** *************@i 40g2000cwc.goog legroups.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(str ReportName As String)
On Error GoTo PrintReport_Err

'Declare variables
Dim Error_Message As Variant
Dim objPrinter As Object
Dim prtDefaultPrint er As Printer

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

'Get default printer
prtDefaultPrint er = Application.Pri nter

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

'Change printer from default to "REPORT_PRINTER "
For Each objPrinter In Application.Pri nters
If objPrinter.Devi ceName = REPORT_PRINTER Then
Set Application.Pri nter = objPrinter
End If
Next

'Change "REPORT_PRINTER " properties
With Reports(strRepo rtName).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.Pri nter = prtDefaultPrint er

PrintReport_End :
Exit Sub

PrintReport_Err :
Select Case Err.Number
Case NO_PRINTER_INST ALLED
Error_Message = MsgBox(e_Messag e_001, vbOKOnly + vbInformation)
Resume PrintReport_End
Case Else
MsgBox Prompt:=Err.Des cription, Buttons:=vbCrit ical & 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
5893
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 example code or hand-holding on the web integration portion. Anyone? "Aaron_TekRecycle.com" <aaron_NAME@DOMAIN_tekrecycle.com> wrote in message
1
1491
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 printers in my printers & faxes. However, when I use the WMI to retrieve the printers in my system, it only retrieve 13 printers instead of 16.
7
14756
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 records that haven't been billed, issue them an invoice, then mark the billed field in those records to yes. Any ideas? Thanks in advance!
4
50258
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. How do I send the out of a report, using VBA, to a specific printer? Michael Charney *** Sent via Developersdex http://www.developersdex.com *** Don't just participate in USENET...get rewarded for it!
5
7123
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 the printer destination for special reports such as stickers and mailing labels. The Mailing Label report is, of course, designed here for a specific printer (Mailing Label). At another site, the name could be completely different. Other than...
7
9080
by: trint | last post by:
How can I add all the network printers to a combobox? Thanks, Trint
0
1588
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 get printer info. problem here i m facing is , i m getting the printer info and am able to write the complete document in that case it is working fine..but while generation of document if it gets fail due to some runtime error, in the immediate run...
25
30991
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
5276
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 DirectoryEntry(strPath); DirectorySearcher mySearcher = new DirectorySearcher(entry); mySearcher.Filter = "(objectCategory=printer)" foreach(SearchResult result in mySearcher.FindAll()) { strName = result.GetDirectoryEntry().Name;
0
9656
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9498
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10370
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10113
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9969
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7519
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6750
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5402
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2896
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.