473,722 Members | 2,338 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Automatically printing a Word document (w/o user interviention)

19 New Member
Hello experts! I'm having trouble coding a button on a form and could use some help.

In a nutshell, I'm attempting to code the button to do the following:

1. Choose the appropriate Word document
2. Fill in form fields on the doc using fields from the record
3. Print the document.
4. Close the word document suppressing any save dialogs.

I'd like to do all of this without user intervention (after clicking the button, of course). The problem I'm having is steps 3 and 4. How do I code it to print the Word document (using the default printer) and close it while suppressing any save dialogs?

Here's the snippet from the relevent section of the code:

Expand|Select|Wrap|Line Numbers
  1. With appWord
  2.  
  3.     Set doc = .Documents.Open(DOC_PATH & DOC_NAME, , True)
  4.     Set rst = New ADODB.Recordset
  5.  
  6.     With doc
  7.         .FormFields("fldFNAME").Result = Nz(Me!txtFNAME)
  8.         .FormFields("fldLNAME").Result = Nz(Me!txtLNAME)
  9.         .FormFields("fldPOSIT2").Result = Nz(subContractData!txtPOSIT2)
  10.         .FormFields("fldSCHNAME1").Result = Nz(subContractData!txtSCHNAME1)
  11.         .FormFields("fldCONTRACTPAY").Result = Nz(subContractData!numCONTRACTPAY)
  12.         .FormFields("fldCONTRACTLENGTH").Result = Nz(subContractData!numCONTRACTLENGTH)
  13.         .FormFields("fldCONTRACTDATE").Result = Nz(dteCONTRACTDATE, Date)
  14.     End With
  15.     .Visible = True
  16.     .Activate
  17. End With
  18.  
Any suggestions are welcomed! TIA

William
Apr 24 '07
17 8709
wparrott
19 New Member
Actually number of copies was easy. Just added another inputbox and used that value to set the .ActiveDocument .Application.Pr intOut Copies:=numCOPI ES option.
Apr 25 '07 #11
ADezii
8,834 Recognized Expert Expert
If I use .ActiveDocument .Close (wdDoNotSaveCha nges) instead of .Quit (wdDoNotSaveCha nges) it will print then close the document window in Word. However Word is still open. Not exactly what I am trying to do but closer.

On another note, is there a way to programmaticall y set the number of copies? I'd like to add a 2nd inputbox to allow multiple copies to be printed.
Are you setting the Object Variable referring to the Instance of Word to nothing?
Expand|Select|Wrap|Line Numbers
  1. Set wdApp = Nothing
Apr 25 '07 #12
wparrott
19 New Member
Are you setting the Object Variable referring to the Instance of Word to nothing?
Expand|Select|Wrap|Line Numbers
  1. Set wdApp = Nothing
Yes, after the last With/End With and right before the Exit Sub. I called it appWord, not wdApp. Would this make any difference?

Expand|Select|Wrap|Line Numbers
  1. Dim appWord As Word.Application
  2.  
Expand|Select|Wrap|Line Numbers
  1. End With
  2.  
  3. Set rst = Nothing
  4. Set doc = Nothing
  5. Set appWord = Nothing
  6. Exit Sub
Apr 25 '07 #13
ADezii
8,834 Recognized Expert Expert
Yes, after the last With/End With and right before the Exit Sub. I called it appWord, not wdApp. Would this make any difference?

Expand|Select|Wrap|Line Numbers
  1. Dim appWord As Word.Application
  2.  
Expand|Select|Wrap|Line Numbers
  1. End With
  2.  
  3. Set rst = Nothing
  4. Set doc = Nothing
  5. Set appWord = Nothing
  6. Exit Sub
The actual Object variable name makes no difference as long as it is the same as the Declaration.
Apr 25 '07 #14
wparrott
19 New Member
Thanks for the help, ADezii. I've got it doing everything that I'd hoped except that it leaves MS-Word open and minimized. This is acceptable since it will require only minimal user intervention and the contract document is closed automatically w/o saving the changes. Now I will move on to my next problem and will post it as a new discussion.
Apr 27 '07 #15
ADezii
8,834 Recognized Expert Expert
Thanks for the help, ADezii. I've got it doing everything that I'd hoped except that it leaves MS-Word open and minimized. This is acceptable since it will require only minimal user intervention and the contract document is closed automatically w/o saving the changes. Now I will move on to my next problem and will post it as a new discussion.
Anytime. It just puzzles me since you obviously conquered the more difficult aspects but the simple task of closing Word remains. Even if the Object Variable (appWord) is not set to Nothing, simply exiting the Print Sub-Routine would make it lose its scope, thereby destroying the Instance of Word. The more I think about it, the more puzzling it gets. Good luck.
Apr 27 '07 #16
wparrott
19 New Member
Update:

I accidently stumbled across the solution to the problem of not being able to close Word after the document prints.

Expand|Select|Wrap|Line Numbers
  1. 'Code to Print document and close Word after printing
  2.     .Visible = True
  3.     .ActiveDocument.Application.Options.PrintBackground = False
  4.     .ActiveDocument.Application.PrintOut Copies:=numCOPIES
  5.     .DisplayAlerts = wdAlertsNone
  6.     .Quit (wdDoNotSaveChanges)
  7.  
The missing line was the 'PrintBackgroun d = False' property. I was trying to print then close Word. Apparently there's a problem with background printing and performing these steps so the solution was to turn off background printing. The command button works fine now. There is a slight delay returning the focus back to the form while the document is printing but that's the only issue I see.

Thanks again, ADezii....you'v e been most helpful to this novice!

William
May 3 '07 #17
ADezii
8,834 Recognized Expert Expert
Update:

I accidently stumbled across the solution to the problem of not being able to close Word after the document prints.

Expand|Select|Wrap|Line Numbers
  1. 'Code to Print document and close Word after printing
  2.     .Visible = True
  3.     .ActiveDocument.Application.Options.PrintBackground = False
  4.     .ActiveDocument.Application.PrintOut Copies:=numCOPIES
  5.     .DisplayAlerts = wdAlertsNone
  6.     .Quit (wdDoNotSaveChanges)
  7.  
The missing line was the 'PrintBackgroun d = False' property. I was trying to print then close Word. Apparently there's a problem with background printing and performing these steps so the solution was to turn off background printing. The command button works fine now. There is a slight delay returning the focus back to the form while the document is printing but that's the only issue I see.

Thanks again, ADezii....you'v e been most helpful to this novice!

William
No problem, way to stick with it and arrive at a solution! I commend your efforts!
May 3 '07 #18

Sign in to post your reply or Sign up for a free account.

Similar topics

1
380
by: ram | last post by:
how do I open a word document within a webform? I need to open the document within the form so the user can edit the document and then click on a button on the form (so that the document is automatically saved to a specified location on the server). thank you!
0
1651
by: Youss33 | last post by:
I have a problem printing a word document from an ASP.NET application. the Application doesn't send an error back but the document is not printed and the document is kept open by the application (I can't kill the winword procees: access denied). her is the code i use: oWordApp = New Word.ApplicationClass oWordDoc = oWordApp.Documents.Add("d:\\coucou2.doc") oWordDoc.Activate() oWordDoc.PrintOut() oWordDoc.Close()
2
4313
by: Vivek | last post by:
Hi, I have an application that writes to a word document. After that I wish to print the document to a particular printer automatically. I tried but it keeps on printing to the default printer. How can I set the printer for the word document to print? Thanks
1
4975
by: rija | last post by:
Hi all, I would like to know how can I do to display print dialog so that the user can choose printer when printing word document object. Basically, I download rtf document on the HD and open it with VB.NET then use printout method to print it. Now, what I want to do is to make the print dialog available.
9
3468
by: ewolfman | last post by:
Hi, I need to provide the user with the option to click something, and what follows is: 1. Download a word document, without displaying winword. 2. Printing the document to the default printer (no print dialog should pop). 3. Close word. All of this must be completely "transparent" to the user, just like a
4
8326
by: Charlie Brookhart | last post by:
I am trying to write a code for a button click event. When the button is clicked, it is supposed to bring up an open file dialog box to allow the user to select the document they which to open. That word doucment will then be opened and displayed in MS Word. The code I have below doesn't quite work the way I expected it to. First, VB is complaining that the variable readOnly is not declared. How can that be when it is declared as Dim...
1
7546
by: Laurent Navarro | last post by:
Hi, I created a C# application which opens a Word document, fills some fields and sends the whole document to the printer. Everything is working great but I find the printing step very slow. Each printing order takes about 2 or 3 seconds to complete which can be long when you have 200 different documents to print. I'm using Word because I want the users to be able to easily create some templates containing specials fields that the...
2
9435
by: ClearCut | last post by:
I have written a program in VB6 that opens an existing Word document and adds some text to the top of the document before printing it. Now I want to close the document without saving the changes. This preserves the document as a "templet". However it seems that it automatically saves the document with the changes prior to closing. Below is my code. Thanks in advance for your help. Dim strE As String Dim objWord As Word.Application ...
3
3243
by: Greg (codepug | last post by:
For the purpose of viewing a document that I would like to modify on rare occassion using word. I would like to create a document with MS Word, and then have my Access Application launch and display the document in a way that allows the user to view the document, but prohibits the document from being changed. I used HyperLink to attempt this, but the Word application is automatically launched, and the user can alter the document....
0
8863
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
8739
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
9384
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...
0
9238
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9157
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
9088
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...
0
8052
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6681
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...
3
2147
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.