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
Apr 24 '07
17 8709
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.
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?
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
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? - 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.
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.
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 '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
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. -
'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 '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!
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: 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,...
| |
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: 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...
|
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,...
|
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: 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...
| |
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...
| |