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 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: - 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.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.
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: - 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.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?
__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.
ADezii 8,834
Recognized Expert Expert
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?
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): - 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 (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.
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 automatically saved to a specified location on the server). thank you!
|
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()
|
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
|
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.
|
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
| |
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...
|
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...
|
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
...
|
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....
|
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...
|
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...
| |
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...
|
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...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |