473,761 Members | 5,848 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Automate the closing of Word from Access after MailMerge

I can't get ths code to correctly execute. After I have clicked on a
Access Form, I want the code to open the word document, merge the data,
print the forms and then close the two windows that open of Word.

The Mail Merge takes place, and the sticking point is that I have to
manually close the two Word Windows.

Can someone please tell me what I'm not doing correctly. Below is the
code I am using...

Thanks,

Ray.
objWord.Activat e
objWord.Close SaveChanges:=wd DoNotSaveChange s
objWord.Applica tion.Quit
Set objWord = Nothing

Nov 13 '05 #1
4 12053
The two Word windows?
Why do you have two opened?

Is there any user interaction with the Word applications that are opened
before they are to close?

Perhaps posting more of your code (i.e. all of it) might provide more
insight?

(for example I personally like to have a Word application object and not
just refer to it through the document.applic ation)


"Ray Todd Jr" <sc*********@co mcast.net> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
I can't get ths code to correctly execute. After I have clicked on a
Access Form, I want the code to open the word document, merge the data,
print the forms and then close the two windows that open of Word.

The Mail Merge takes place, and the sticking point is that I have to
manually close the two Word Windows.

Can someone please tell me what I'm not doing correctly. Below is the
code I am using...

Thanks,

Ray.
objWord.Activat e
objWord.Close SaveChanges:=wd DoNotSaveChange s
objWord.Applica tion.Quit
Set objWord = Nothing

Nov 13 '05 #2
I'll post the entire code below. When I click on the button (that the
code is attached to), it opens up two instances...:

Any advice/suggestions you can provide would be helpful...

Ray.

Private Sub PrintSummons_Cl ick()

Dim objWord As Word.Document
Set objWord = GetObject("e:\S UMMONSFORM.DOC" , "Word.Document" )

'Make Word Visible
objWord.Applica tion.Visible = True

'Set Merge Data Source
objWord.MailMer ge.OpenDataSour ce Name:="E:\CityT axSaleV63.mdb", _
LinktoSource:=T rue, _
Connection:="QU ERY qryOWNERCODEFEN DANTsummonsmerg edata", _
SQLStatement:=" SELECT * FROM qryOWNERCODEFEN DANTsummonsmerg edata"

'Execute the MailMerge
objWord.MailMer ge.Destination = wdSendToNewDocu ment
objWord.MailMer ge.Execute
objWord.Applica tion.Options.Pr intBackground = False
objWord.Applica tion.ActiveDocu ment.PrintOut

'Close The form files and the merged document
objWord.Close Word.wdDoNotSav eChanges
Set objWord = Nothing
End Sub

Nov 13 '05 #3
How about something like...
(Perhaps switching the three strings to passed arguments to make it a little
more versatile?)

Function MergeIt()

On Error GoTo ErrHandling

Dim objDoc As Word.Document
Dim objWord As Word.Applicatio n
Dim blnCreated As Boolean
Dim strFilename As String
Dim strQueryName As String
Dim strDBpath As String

strFilename = "e:\SUMMONSFORM .DOC"
strQueryName = "qryOWNERCODEFE NDANTsummonsmer gedata"
strDBpath = "E:\CityTaxSale V63.mdb" 'Or probably just CurrentDb.Name

On Error Resume Next
Set objWord = GetObject(, "Word.Applicati on")
If Err Then
Set objWord = CreateObject("W ord.Application ")
blnCreated = True
End If

On Error GoTo ErrHandling

Set objDoc = objWord.Documen ts.Open(strFile name)

'Make Word Visible
objWord.Visible = True

'Execute the MailMerge
With objDoc.MailMerg e
'Set Merge Data Source
objDoc.MailMerg e.OpenDataSourc e Name:=strDBpath , _
LinktoSource:=T rue, _
Connection:="QU ERY " & strQueryName, _
SQLStatement:=" SELECT * FROM " & strQueryName
.Destination = wdSendToNewDocu ment
.Execute
objWord.ActiveD ocument.PrintOu t False
objWord.ActiveD ocument.Close wdDoNotSaveChan ges
End With

'Close The form files and the merged document
objDoc.Close wdDoNotSaveChan ges

If blnCreated Then
objWord.Quit
End If

Set objDoc = Nothing
Set objWord = Nothing

Exit Function

ErrHandling:
msgbox "Whoops" 'Better error handling of course
End Function

"Ray Todd Jr" <sc*********@co mcast.net> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
I'll post the entire code below. When I click on the button (that the
code is attached to), it opens up two instances...:

Any advice/suggestions you can provide would be helpful...

Ray.

Private Sub PrintSummons_Cl ick()

Dim objWord As Word.Document
Set objWord = GetObject("e:\S UMMONSFORM.DOC" , "Word.Document" )

'Make Word Visible
objWord.Applica tion.Visible = True

'Set Merge Data Source
objWord.MailMer ge.OpenDataSour ce Name:="E:\CityT axSaleV63.mdb", _
LinktoSource:=T rue, _
Connection:="QU ERY qryOWNERCODEFEN DANTsummonsmerg edata", _
SQLStatement:=" SELECT * FROM qryOWNERCODEFEN DANTsummonsmerg edata"

'Execute the MailMerge
objWord.MailMer ge.Destination = wdSendToNewDocu ment
objWord.MailMer ge.Execute
objWord.Applica tion.Options.Pr intBackground = False
objWord.Applica tion.ActiveDocu ment.PrintOut

'Close The form files and the merged document
objWord.Close Word.wdDoNotSav eChanges
Set objWord = Nothing
End Sub

Nov 13 '05 #4
Leigh,

That worked perfectly. Thank you for both your time and expertise.

Again thanks,

Ray.

Nov 13 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2070
by: Dan Nash | last post by:
Hi guys I wonder if you could help. I'm trying to create a bespoke interface for mail merging from an Access database in Word. At the moment, I'm just trying it with CSV files, and it works. Word loads, data gets pasted in and it merges fine. One problem tho, it's doing Form Letters rather than Mailing Labels Here's the code...
3
3076
by: Lynn A. | last post by:
Access/Word 2000 I am trying to merge a word document with an access query that has a parameter attached to it. Could someone please suggest how to stop the parameter from appearing when the query runs. I think it might have something to do with the source document mailmerge.doc. Could it be that I'm trying to run the query from
1
4959
by: svdh | last post by:
I have posed a question last saturday and have advanced alot in the meantime. But I am still not there Problem is that I try to merging various fields from various tables in one document in Word 1. Query..I want to keep the fields seperatred. I do not want to sent on field with all accumulated languages from one person to Word. Each language should appear in the document in a separate cell Cross tables are not delivering the result I...
0
2185
by: Linda Cacina | last post by:
Hello all you fine folks, Here is some code I am using to merge data from a single record Access 2K3 table into a NEW word document based on a pre-defined Word merge template doc. All I want to happen is as follows: User defines a storage path and name for the new Word document then clicks a "Create" button on a form. From that point on I use the following code:
4
7472
by: pmhaupt2 | last post by:
I developed an Access 2003 program that will allow the user to produce a group of Word letters that merge with data records from an Access database. I created a mail merge Word document and associated it with a specific query in my Access database. My problem is that when I run my "create letters" button from an Access form, the program opens up Word correctly with the proper letter. However, the MailMerge toolbar in Word appears...
10
26093
by: John | last post by:
Hi I am trying to do a word mailmerge form within my vb.net app. My problem is how to do a query on one of my tables and use the result as the mail merge datasource. Any help would be appreciated. Thanks Regards
5
3724
by: johnvon | last post by:
I have a mail merge that runs flawlessly in Access 2000. In 2003 I keep getting dialog boxes that I can't figure out how to supress. Several of them are telling me that Admin (me!) has the database opened (duh!). I then get a logon dialog box, which I cancel. The merge then runs perfectly! How do I supress all of these dialog boxes. Thanks! John
5
2493
by: TriciaL | last post by:
Hi all, I've been going through the threads on the Mergit() function to run a Word mailmerge out of Access, and I've hit a roadblock. The first problem I had was discussed in this thread: http://www.thescripts.com/forum/thread626840-mailmerge+word+access.html So as instructed, I added a reference to Microsoft Word. On the bright side, at least I'm getting a different error than I was before (but I still can't get it to work) Here's the...
1
6546
by: Esther Lane | last post by:
Hello! First off, many many thanks to Albert who wrote the Mail Merge code for MS Access I am using. It has been working beautifully for a few years. However, my client just (without notice!) upgraded from Access 2000 to Access 2007. Now that component is failing. The merge is building the data source file fine (text file named merge.888). I am providing an absolute path reference to the word file being used for the mail merge. ...
0
9522
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
9948
jinu1996
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...
1
9902
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,...
0
8770
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
6603
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
5364
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3866
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
3
3446
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2738
bsmnconsultancy
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...

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.