423,688 Members | 2,147 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,688 IT Pros & Developers. It's quick & easy.

Automate the closing of Word from Access after MailMerge

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
Leigh,

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

Again thanks,

Ray.

Nov 13 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.