473,321 Members | 1,667 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

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.Activate
objWord.Close SaveChanges:=wdDoNotSaveChanges
objWord.Application.Quit
Set objWord = Nothing

Nov 13 '05 #1
4 12024
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.application)


"Ray Todd Jr" <sc*********@comcast.net> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.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.Activate
objWord.Close SaveChanges:=wdDoNotSaveChanges
objWord.Application.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_Click()

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

'Make Word Visible
objWord.Application.Visible = True

'Set Merge Data Source
objWord.MailMerge.OpenDataSource Name:="E:\CityTaxSaleV63.mdb", _
LinktoSource:=True, _
Connection:="QUERY qryOWNERCODEFENDANTsummonsmergedata", _
SQLStatement:="SELECT * FROM qryOWNERCODEFENDANTsummonsmergedata"

'Execute the MailMerge
objWord.MailMerge.Destination = wdSendToNewDocument
objWord.MailMerge.Execute
objWord.Application.Options.PrintBackground = False
objWord.Application.ActiveDocument.PrintOut

'Close The form files and the merged document
objWord.Close Word.wdDoNotSaveChanges
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.Application
Dim blnCreated As Boolean
Dim strFilename As String
Dim strQueryName As String
Dim strDBpath As String

strFilename = "e:\SUMMONSFORM.DOC"
strQueryName = "qryOWNERCODEFENDANTsummonsmergedata"
strDBpath = "E:\CityTaxSaleV63.mdb" 'Or probably just CurrentDb.Name

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

On Error GoTo ErrHandling

Set objDoc = objWord.Documents.Open(strFilename)

'Make Word Visible
objWord.Visible = True

'Execute the MailMerge
With objDoc.MailMerge
'Set Merge Data Source
objDoc.MailMerge.OpenDataSource Name:=strDBpath, _
LinktoSource:=True, _
Connection:="QUERY " & strQueryName, _
SQLStatement:="SELECT * FROM " & strQueryName
.Destination = wdSendToNewDocument
.Execute
objWord.ActiveDocument.PrintOut False
objWord.ActiveDocument.Close wdDoNotSaveChanges
End With

'Close The form files and the merged document
objDoc.Close wdDoNotSaveChanges

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*********@comcast.net> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.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_Click()

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

'Make Word Visible
objWord.Application.Visible = True

'Set Merge Data Source
objWord.MailMerge.OpenDataSource Name:="E:\CityTaxSaleV63.mdb", _
LinktoSource:=True, _
Connection:="QUERY qryOWNERCODEFENDANTsummonsmergedata", _
SQLStatement:="SELECT * FROM qryOWNERCODEFENDANTsummonsmergedata"

'Execute the MailMerge
objWord.MailMerge.Destination = wdSendToNewDocument
objWord.MailMerge.Execute
objWord.Application.Options.PrintBackground = False
objWord.Application.ActiveDocument.PrintOut

'Close The form files and the merged document
objWord.Close Word.wdDoNotSaveChanges
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
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....
3
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...
1
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...
0
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...
4
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...
10
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...
5
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...
5
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:...
1
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!)...
0
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
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...
0
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...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.