469,609 Members | 2,214 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,609 developers. It's quick & easy.

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

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 8229
ADezii
8,800 Expert 8TB
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
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.ActiveDocument.Close (wdDoNotSaveChanges)
' 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,800 Expert 8TB
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.ActiveDocument.Close (wdDoNotSaveChanges)
' 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
__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,800 Expert 8TB
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
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,800 Expert 8TB
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
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
If I use .ActiveDocument.Close (wdDoNotSaveChanges) instead of .Quit (wdDoNotSaveChanges) 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 programmatically set the number of copies? I'd like to add a 2nd inputbox to allow multiple copies to be printed.
Apr 25 '07 #10
Actually number of copies was easy. Just added another inputbox and used that value to set the .ActiveDocument.Application.PrintOut Copies:=numCOPIES option.
Apr 25 '07 #11
ADezii
8,800 Expert 8TB
If I use .ActiveDocument.Close (wdDoNotSaveChanges) instead of .Quit (wdDoNotSaveChanges) 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 programmatically 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
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,800 Expert 8TB
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
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,800 Expert 8TB
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
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 'PrintBackground = 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've been most helpful to this novice!

William
May 3 '07 #17
ADezii
8,800 Expert 8TB
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 'PrintBackground = 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've 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

Post your reply

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

Similar topics

1 post views Thread by ram | last post: by
reply views Thread by Youss33 | last post: by
9 posts views Thread by ewolfman | last post: by
4 posts views Thread by Charlie Brookhart | last post: by
1 post views Thread by Laurent Navarro | last post: by
3 posts views Thread by Greg (codepug | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.