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: - With appWord
-
-
Set doc = .Documents.Open(DOC_PATH & DOC_NAME, , True)
-
Set rst = New ADODB.Recordset
-
-
With doc
-
.FormFields("fldFNAME").Result = Nz(Me!txtFNAME)
-
.FormFields("fldLNAME").Result = Nz(Me!txtLNAME)
-
.FormFields("fldPOSIT2").Result = Nz(subContractData!txtPOSIT2)
-
.FormFields("fldSCHNAME1").Result = Nz(subContractData!txtSCHNAME1)
-
.FormFields("fldCONTRACTPAY").Result = Nz(subContractData!numCONTRACTPAY)
-
.FormFields("fldCONTRACTLENGTH").Result = Nz(subContractData!numCONTRACTLENGTH)
-
.FormFields("fldCONTRACTDATE").Result = Nz(dteCONTRACTDATE, Date)
-
End With
-
.Visible = True
-
.Activate
-
End With
-
Any suggestions are welcomed! TIA
William
17 8646
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: - With appWord
-
-
Set doc = .Documents.Open(DOC_PATH & DOC_NAME, , True)
-
Set rst = New ADODB.Recordset
-
-
With doc
-
.FormFields("fldFNAME").Result = Nz(Me!txtFNAME)
-
.FormFields("fldLNAME").Result = Nz(Me!txtLNAME)
-
.FormFields("fldPOSIT2").Result = Nz(subContractData!txtPOSIT2)
-
.FormFields("fldSCHNAME1").Result = Nz(subContractData!txtSCHNAME1)
-
.FormFields("fldCONTRACTPAY").Result = Nz(subContractData!numCONTRACTPAY)
-
.FormFields("fldCONTRACTLENGTH").Result = Nz(subContractData!numCONTRACTLENGTH)
-
.FormFields("fldCONTRACTDATE").Result = Nz(dteCONTRACTDATE, Date)
-
End With
-
.Visible = True
-
.Activate
-
End With
-
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)
- Application.ActiveDocument.Printout
close it while suppressing any save dialogs?
- Application.DisplayAlerts = wdAlertsNone
Thanks ADezii for the reply.
I did some experimenting and it appears that if I run it like this: - With appWord
-
-
Set doc = .Documents.Open(DOC_PATH & DOC_NAME, , True)
-
Set rst = New ADODB.Recordset
-
-
With doc
-
.FormFields("fldFNAME").Result = Nz(Me!txtFNAME)
-
.FormFields("fldLNAME").Result = Nz(Me!txtLNAME)
-
.FormFields("fldPOSIT2").Result = Nz(subContractData!txtPOSIT2)
-
.FormFields("fldSCHNAME1").Result = Nz(subContractData!txtSCHNAME1)
-
.FormFields("fldCONTRACTPAY").Result = Nz(subContractData!numCONTRACTPAY)
-
.FormFields("fldCONTRACTLENGTH").Result = Nz(subContractData!numCONTRACTLENGTH)
-
.FormFields("fldCONTRACTDATE").Result = Nz(dteCONTRACTDATE, Date)
-
End With
-
.PrintOut
-
-
-
End With
-
Set rst = Nothing
-
Set doc = Nothing
-
Set appWord = Nothing
-
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.
Thanks ADezii for the reply.
I did some experimenting and it appears that if I run it like this: - With appWord
-
-
Set doc = .Documents.Open(DOC_PATH & DOC_NAME, , True)
-
Set rst = New ADODB.Recordset
-
-
With doc
-
.FormFields("fldFNAME").Result = Nz(Me!txtFNAME)
-
.FormFields("fldLNAME").Result = Nz(Me!txtLNAME)
-
.FormFields("fldPOSIT2").Result = Nz(subContractData!txtPOSIT2)
-
.FormFields("fldSCHNAME1").Result = Nz(subContractData!txtSCHNAME1)
-
.FormFields("fldCONTRACTPAY").Result = Nz(subContractData!numCONTRACTPAY)
-
.FormFields("fldCONTRACTLENGTH").Result = Nz(subContractData!numCONTRACTLENGTH)
-
.FormFields("fldCONTRACTDATE").Result = Nz(dteCONTRACTDATE, Date)
-
End With
-
.PrintOut
-
-
-
End With
-
Set rst = Nothing
-
Set doc = Nothing
-
Set appWord = Nothing
-
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?
__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: - End With
-
.Visible = True
-
.PrintOut
-
.Application.DisplayAlerts = wdAlertsNone
-
.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.
Yes, I tried it like this: - End With
-
.Visible = True
-
.PrintOut
-
.Application.DisplayAlerts = wdAlertsNone
-
.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.
- 'Try revising the code as follows:
-
With wdApp
-
.Visible = True
-
.ActiveDocument.PrintOut
-
.DisplayAlerts = wdAlertsNone
-
.Quit (wdDoNotSaveChanges)
-
End With
-
-
Set wdApp = Nothing
Same result. Word opens then closes without printing. Here's the code I have so far (everything is working as expected except for printing): - Private Sub cmdPrintContract_Click()
-
-
Dim appWord As Word.Application
-
Dim doc As Word.Document
-
Dim DOC_NAME As String
-
Dim rst As ADODB.Recordset
-
Dim strSQL As String
-
Dim dteCONTRACTDATE1 As Date
-
Dim dteCONTRACTDATE2 As Date
-
Dim dteCONTRACTDATE3 As Date
-
Dim Message, Title, Default
-
-
'Inputbox variables
-
Message = "Enter Contract Signature Date or Press Enter for Today"
-
Title = "Contract Signature Date"
-
Default = Date
-
' Get contract signature date from user.
-
dteCONTRACTDATE1 = InputBox(Message, Title, Default)
-
' This passes the contract signature date to 2 other
-
' lines in the Word document.
-
dteCONTRACTDATE2 = dteCONTRACTDATE1
-
dteCONTRACTDATE3 = dteCONTRACTDATE1
-
-
'Determine which contract form to use for current record
-
Select Case subContractData!numCONTRACTCODE
-
-
Case 1
-
DOC_NAME = "annual.doc"
-
Case 2
-
DOC_NAME = "classified.doc"
-
Case 3
-
DOC_NAME = "busdriver.doc"
-
Case 4
-
DOC_NAME = "continuing.doc"
-
Case 5
-
DOC_NAME = "local.doc"
-
Case 6
-
DOC_NAME = "supp2continuing.doc"
-
Case 7
-
DOC_NAME = "supplement.doc"
-
End Select
-
-
On Error Resume Next
-
Set appWord = GetObject(, "Word.application")
-
If Err = 429 Then
-
Set appWord = New Word.Application
-
Err = 0
-
End If
-
'Open Word document and populate form fields using current record
-
With appWord
-
-
Set doc = .Documents.Open(DOC_PATH & DOC_NAME, , True)
-
Set rst = New ADODB.Recordset
-
-
With doc
-
.FormFields("fldFNAME").Result = Nz(Me!txtFNAME)
-
.FormFields("fldLNAME").Result = Nz(Me!txtLNAME)
-
.FormFields("fldPOSIT2").Result = Nz(subContractData!txtPOSIT2)
-
.FormFields("fldSCHNAME1").Result = Nz(subContractData!txtSCHNAME1)
-
.FormFields("fldCONTRACTPAY").Result = Nz(subContractData!numCONTRACTPAY)
-
.FormFields("fldCONTRACTLENGTH").Result = Nz(subContractData!numCONTRACTLENGTH)
-
.FormFields("fldCONTRACTDATE1").Result = Nz(dteCONTRACTDATE1, Date)
-
.FormFields("fldCONTRACTDATE2").Result = Nz(dteCONTRACTDATE2, Date)
-
.FormFields("fldCONTRACTDATE3").Result = Nz(dteCONTRACTDATE3, Date)
-
End With
-
-
'Print contract form and close Word without user intervention
-
-
.Visible = True
-
.ActiveDocument.PrintOut
-
.DisplayAlerts = wdAlertsNone
-
.Quit (wdDoNotSaveChanges)
-
-
-
End With
-
-
Set rst = Nothing
-
Set doc = Nothing
-
Set appWord = Nothing
-
Exit Sub
-
-
ErrorHandler:
-
MsgBox Err & ": " & Err.Description
-
-
End Sub
-
Perhaps I missed something?
Same result. Word opens then closes without printing. Here's the code I have so far (everything is working as expected except for printing): - Private Sub cmdPrintContract_Click()
-
-
Dim appWord As Word.Application
-
Dim doc As Word.Document
-
Dim DOC_NAME As String
-
Dim rst As ADODB.Recordset
-
Dim strSQL As String
-
Dim dteCONTRACTDATE1 As Date
-
Dim dteCONTRACTDATE2 As Date
-
Dim dteCONTRACTDATE3 As Date
-
Dim Message, Title, Default
-
-
'Inputbox variables
-
Message = "Enter Contract Signature Date or Press Enter for Today"
-
Title = "Contract Signature Date"
-
Default = Date
-
' Get contract signature date from user.
-
dteCONTRACTDATE1 = InputBox(Message, Title, Default)
-
' This passes the contract signature date to 2 other
-
' lines in the Word document.
-
dteCONTRACTDATE2 = dteCONTRACTDATE1
-
dteCONTRACTDATE3 = dteCONTRACTDATE1
-
-
'Determine which contract form to use for current record
-
Select Case subContractData!numCONTRACTCODE
-
-
Case 1
-
DOC_NAME = "annual.doc"
-
Case 2
-
DOC_NAME = "classified.doc"
-
Case 3
-
DOC_NAME = "busdriver.doc"
-
Case 4
-
DOC_NAME = "continuing.doc"
-
Case 5
-
DOC_NAME = "local.doc"
-
Case 6
-
DOC_NAME = "supp2continuing.doc"
-
Case 7
-
DOC_NAME = "supplement.doc"
-
End Select
-
-
On Error Resume Next
-
Set appWord = GetObject(, "Word.application")
-
If Err = 429 Then
-
Set appWord = New Word.Application
-
Err = 0
-
End If
-
'Open Word document and populate form fields using current record
-
With appWord
-
-
Set doc = .Documents.Open(DOC_PATH & DOC_NAME, , True)
-
Set rst = New ADODB.Recordset
-
-
With doc
-
.FormFields("fldFNAME").Result = Nz(Me!txtFNAME)
-
.FormFields("fldLNAME").Result = Nz(Me!txtLNAME)
-
.FormFields("fldPOSIT2").Result = Nz(subContractData!txtPOSIT2)
-
.FormFields("fldSCHNAME1").Result = Nz(subContractData!txtSCHNAME1)
-
.FormFields("fldCONTRACTPAY").Result = Nz(subContractData!numCONTRACTPAY)
-
.FormFields("fldCONTRACTLENGTH").Result = Nz(subContractData!numCONTRACTLENGTH)
-
.FormFields("fldCONTRACTDATE1").Result = Nz(dteCONTRACTDATE1, Date)
-
.FormFields("fldCONTRACTDATE2").Result = Nz(dteCONTRACTDATE2, Date)
-
.FormFields("fldCONTRACTDATE3").Result = Nz(dteCONTRACTDATE3, Date)
-
End With
-
-
'Print contract form and close Word without user intervention
-
-
.Visible = True
-
.ActiveDocument.PrintOut
-
.DisplayAlerts = wdAlertsNone
-
.Quit (wdDoNotSaveChanges)
-
-
-
End With
-
-
Set rst = Nothing
-
Set doc = Nothing
-
Set appWord = Nothing
-
Exit Sub
-
-
ErrorHandler:
-
MsgBox Err & ": " & Err.Description
-
-
End Sub
-
Perhaps I missed something?
try: - ActiveDocument.ActiveWindow.PrintOut
try: - ActiveDocument.ActiveWindow.PrintOut
Nope. Same result. Word opens then closes w/o printing the document.
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.
Actually number of copies was easy. Just added another inputbox and used that value to set the .ActiveDocument.Application.PrintOut Copies:=numCOPIES option.
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?
Are you setting the Object Variable referring to the Instance of Word to 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? - Dim appWord As Word.Application
-
- End With
-
-
Set rst = Nothing
-
Set doc = Nothing
-
Set appWord = Nothing
-
Exit Sub
Yes, after the last With/End With and right before the Exit Sub. I called it appWord, not wdApp. Would this make any difference? - Dim appWord As Word.Application
-
- End With
-
-
Set rst = Nothing
-
Set doc = Nothing
-
Set appWord = Nothing
-
Exit Sub
The actual Object variable name makes no difference as long as it is the same as the Declaration.
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.
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.
Update:
I accidently stumbled across the solution to the problem of not being able to close Word after the document prints. -
'Code to Print document and close Word after printing
-
.Visible = True
-
.ActiveDocument.Application.Options.PrintBackground = False
-
.ActiveDocument.Application.PrintOut Copies:=numCOPIES
-
.DisplayAlerts = wdAlertsNone
-
.Quit (wdDoNotSaveChanges)
-
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
Update:
I accidently stumbled across the solution to the problem of not being able to close Word after the document prints. -
'Code to Print document and close Word after printing
-
.Visible = True
-
.ActiveDocument.Application.Options.PrintBackground = False
-
.ActiveDocument.Application.PrintOut Copies:=numCOPIES
-
.DisplayAlerts = wdAlertsNone
-
.Quit (wdDoNotSaveChanges)
-
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!
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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....
|
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....
|
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. ...
|
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...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
| |