473,698 Members | 2,275 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 #1
17 8701
ADezii
8,834 Recognized Expert Expert
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
Do not take this as Gospel Truth, but it will point you in the right direction:
How do I code it to print the Word document (using the default printer)
Expand|Select|Wrap|Line Numbers
  1. Application.ActiveDocument.Printout
close it while suppressing any save dialogs?
Expand|Select|Wrap|Line Numbers
  1. Application.DisplayAlerts = wdAlertsNone
Apr 25 '07 #2
wparrott
19 New Member
Thanks ADezii for the reply.

I did some experimenting and it appears that if I run it like this:
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.     .PrintOut
  16.  
  17.  
  18. End With
  19. Set rst = Nothing
  20. Set doc = Nothing
  21. Set appWord = Nothing
  22. Exit Sub
it prints without displaying Word. However, Word is still running in the background (I can see it in the process list on task manager). This can't be good!
I tried adding 'appWord.Active Document.Close (wdDoNotSaveCha nges)
' after the last End With and before the Set commands but that didn't work either. Half the problem is licked so far.

Any additional help is appreciated.
Apr 25 '07 #3
ADezii
8,834 Recognized Expert Expert
Thanks ADezii for the reply.

I did some experimenting and it appears that if I run it like this:
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.     .PrintOut
  16.  
  17.  
  18. End With
  19. Set rst = Nothing
  20. Set doc = Nothing
  21. Set appWord = Nothing
  22. Exit Sub
it prints without displaying Word. However, Word is still running in the background (I can see it in the process list on task manager). This can't be good!
I tried adding 'appWord.Active Document.Close (wdDoNotSaveCha nges)
' after the last End With and before the Set commands but that didn't work either. Half the problem is licked so far.

Any additional help is appreciated.
__1. By Default, the Automation Object is not Visible. Simply sett appWord.Visible = True prior to printing and you should be OK.
__2. Did you try DisplayAlerts?
Apr 25 '07 #4
wparrott
19 New Member
__1. By Default, the Automation Object is not Visible. Simply sett appWord.Visible = True prior to printing and you should be OK.
__2. Did you try DisplayAlerts?
Yes, I tried it like this:

Expand|Select|Wrap|Line Numbers
  1. End With
  2.     .Visible = True
  3.     .PrintOut
  4.     .Application.DisplayAlerts = wdAlertsNone
  5.     .Application.Quit (wdDoNotSaveChanges)
Am I using the DisplayAlerts correctly? Nothing appears to happen. The document prints but doesn't close. If I close Word manually it prompts to save changes. I added the last line to the code after trying the DisplayAlerts. It closes Word but does not print. I've been searching the web but so far can't find an example that uses both the .PrintOut and .Quit methods together.
Apr 25 '07 #5
ADezii
8,834 Recognized Expert Expert
Yes, I tried it like this:

Expand|Select|Wrap|Line Numbers
  1. End With
  2.     .Visible = True
  3.     .PrintOut
  4.     .Application.DisplayAlerts = wdAlertsNone
  5.     .Application.Quit (wdDoNotSaveChanges)
Am I using the DisplayAlerts correctly? Nothing appears to happen. The document prints but doesn't close. If I close Word manually it prompts to save changes. I added the last line to the code after trying the DisplayAlerts. It closes Word but does not print. I've been searching the web but so far can't find an example that uses both the .PrintOut and .Quit methods together.
Expand|Select|Wrap|Line Numbers
  1. 'Try revising the code as follows:
  2. With wdApp
  3.     .Visible = True
  4.     .ActiveDocument.PrintOut
  5.     .DisplayAlerts = wdAlertsNone
  6.     .Quit (wdDoNotSaveChanges)
  7. End With
  8.  
  9. Set wdApp = Nothing
Apr 25 '07 #6
wparrott
19 New Member
Same result. Word opens then closes without printing. Here's the code I have so far (everything is working as expected except for printing):

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdPrintContract_Click()
  2.  
  3. Dim appWord As Word.Application
  4. Dim doc As Word.Document
  5. Dim DOC_NAME As String
  6. Dim rst As ADODB.Recordset
  7. Dim strSQL As String
  8. Dim dteCONTRACTDATE1 As Date
  9. Dim dteCONTRACTDATE2 As Date
  10. Dim dteCONTRACTDATE3 As Date
  11. Dim Message, Title, Default
  12.  
  13. 'Inputbox variables
  14. Message = "Enter Contract Signature Date or Press Enter for Today"
  15. Title = "Contract Signature Date"
  16. Default = Date
  17. ' Get contract signature date from user.
  18. dteCONTRACTDATE1 = InputBox(Message, Title, Default)
  19. ' This passes the contract signature date to 2 other
  20. ' lines in the Word document.
  21. dteCONTRACTDATE2 = dteCONTRACTDATE1
  22. dteCONTRACTDATE3 = dteCONTRACTDATE1
  23.  
  24. 'Determine which contract form to use for current record
  25. Select Case subContractData!numCONTRACTCODE
  26.  
  27.     Case 1
  28.         DOC_NAME = "annual.doc"
  29.     Case 2
  30.         DOC_NAME = "classified.doc"
  31.     Case 3
  32.         DOC_NAME = "busdriver.doc"
  33.     Case 4
  34.         DOC_NAME = "continuing.doc"
  35.     Case 5
  36.         DOC_NAME = "local.doc"
  37.     Case 6
  38.         DOC_NAME = "supp2continuing.doc"
  39.     Case 7
  40.         DOC_NAME = "supplement.doc"
  41. End Select
  42.  
  43. On Error Resume Next
  44. Set appWord = GetObject(, "Word.application")
  45. If Err = 429 Then
  46.     Set appWord = New Word.Application
  47.     Err = 0
  48. End If
  49. 'Open Word document and populate form fields using current record
  50. With appWord
  51.  
  52.     Set doc = .Documents.Open(DOC_PATH & DOC_NAME, , True)
  53.     Set rst = New ADODB.Recordset
  54.  
  55.         With doc
  56.             .FormFields("fldFNAME").Result = Nz(Me!txtFNAME)
  57.             .FormFields("fldLNAME").Result = Nz(Me!txtLNAME)
  58.             .FormFields("fldPOSIT2").Result = Nz(subContractData!txtPOSIT2)
  59.             .FormFields("fldSCHNAME1").Result = Nz(subContractData!txtSCHNAME1)
  60.             .FormFields("fldCONTRACTPAY").Result = Nz(subContractData!numCONTRACTPAY)
  61.             .FormFields("fldCONTRACTLENGTH").Result = Nz(subContractData!numCONTRACTLENGTH)
  62.             .FormFields("fldCONTRACTDATE1").Result = Nz(dteCONTRACTDATE1, Date)
  63.             .FormFields("fldCONTRACTDATE2").Result = Nz(dteCONTRACTDATE2, Date)
  64.             .FormFields("fldCONTRACTDATE3").Result = Nz(dteCONTRACTDATE3, Date)
  65.         End With
  66.  
  67. 'Print contract form and close Word without user intervention        
  68.  
  69.     .Visible = True
  70.     .ActiveDocument.PrintOut
  71.     .DisplayAlerts = wdAlertsNone
  72.     .Quit (wdDoNotSaveChanges)
  73.  
  74.  
  75. End With
  76.  
  77. Set rst = Nothing
  78. Set doc = Nothing
  79. Set appWord = Nothing
  80. Exit Sub
  81.  
  82. ErrorHandler:
  83.     MsgBox Err & ": " & Err.Description
  84.  
  85. End Sub
  86.  
Perhaps I missed something?
Apr 25 '07 #7
ADezii
8,834 Recognized Expert Expert
Same result. Word opens then closes without printing. Here's the code I have so far (everything is working as expected except for printing):

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdPrintContract_Click()
  2.  
  3. Dim appWord As Word.Application
  4. Dim doc As Word.Document
  5. Dim DOC_NAME As String
  6. Dim rst As ADODB.Recordset
  7. Dim strSQL As String
  8. Dim dteCONTRACTDATE1 As Date
  9. Dim dteCONTRACTDATE2 As Date
  10. Dim dteCONTRACTDATE3 As Date
  11. Dim Message, Title, Default
  12.  
  13. 'Inputbox variables
  14. Message = "Enter Contract Signature Date or Press Enter for Today"
  15. Title = "Contract Signature Date"
  16. Default = Date
  17. ' Get contract signature date from user.
  18. dteCONTRACTDATE1 = InputBox(Message, Title, Default)
  19. ' This passes the contract signature date to 2 other
  20. ' lines in the Word document.
  21. dteCONTRACTDATE2 = dteCONTRACTDATE1
  22. dteCONTRACTDATE3 = dteCONTRACTDATE1
  23.  
  24. 'Determine which contract form to use for current record
  25. Select Case subContractData!numCONTRACTCODE
  26.  
  27.     Case 1
  28.         DOC_NAME = "annual.doc"
  29.     Case 2
  30.         DOC_NAME = "classified.doc"
  31.     Case 3
  32.         DOC_NAME = "busdriver.doc"
  33.     Case 4
  34.         DOC_NAME = "continuing.doc"
  35.     Case 5
  36.         DOC_NAME = "local.doc"
  37.     Case 6
  38.         DOC_NAME = "supp2continuing.doc"
  39.     Case 7
  40.         DOC_NAME = "supplement.doc"
  41. End Select
  42.  
  43. On Error Resume Next
  44. Set appWord = GetObject(, "Word.application")
  45. If Err = 429 Then
  46.     Set appWord = New Word.Application
  47.     Err = 0
  48. End If
  49. 'Open Word document and populate form fields using current record
  50. With appWord
  51.  
  52.     Set doc = .Documents.Open(DOC_PATH & DOC_NAME, , True)
  53.     Set rst = New ADODB.Recordset
  54.  
  55.         With doc
  56.             .FormFields("fldFNAME").Result = Nz(Me!txtFNAME)
  57.             .FormFields("fldLNAME").Result = Nz(Me!txtLNAME)
  58.             .FormFields("fldPOSIT2").Result = Nz(subContractData!txtPOSIT2)
  59.             .FormFields("fldSCHNAME1").Result = Nz(subContractData!txtSCHNAME1)
  60.             .FormFields("fldCONTRACTPAY").Result = Nz(subContractData!numCONTRACTPAY)
  61.             .FormFields("fldCONTRACTLENGTH").Result = Nz(subContractData!numCONTRACTLENGTH)
  62.             .FormFields("fldCONTRACTDATE1").Result = Nz(dteCONTRACTDATE1, Date)
  63.             .FormFields("fldCONTRACTDATE2").Result = Nz(dteCONTRACTDATE2, Date)
  64.             .FormFields("fldCONTRACTDATE3").Result = Nz(dteCONTRACTDATE3, Date)
  65.         End With
  66.  
  67. 'Print contract form and close Word without user intervention        
  68.  
  69.     .Visible = True
  70.     .ActiveDocument.PrintOut
  71.     .DisplayAlerts = wdAlertsNone
  72.     .Quit (wdDoNotSaveChanges)
  73.  
  74.  
  75. End With
  76.  
  77. Set rst = Nothing
  78. Set doc = Nothing
  79. Set appWord = Nothing
  80. Exit Sub
  81.  
  82. ErrorHandler:
  83.     MsgBox Err & ": " & Err.Description
  84.  
  85. End Sub
  86.  
Perhaps I missed something?
try:
Expand|Select|Wrap|Line Numbers
  1. ActiveDocument.ActiveWindow.PrintOut 
Apr 25 '07 #8
wparrott
19 New Member
try:
Expand|Select|Wrap|Line Numbers
  1. ActiveDocument.ActiveWindow.PrintOut 

Nope. Same result. Word opens then closes w/o printing the document.
Apr 25 '07 #9
wparrott
19 New Member
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.
Apr 25 '07 #10

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
1649
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
4311
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
4972
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
3463
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
8322
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
7544
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
9433
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
3238
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
8672
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
9156
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
8860
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
7712
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...
0
5860
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
4361
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...
0
4614
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3038
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 we have to send another system
2
2323
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.