472,374 Members | 1,594 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,374 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 11910
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!)...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...
0
by: F22F35 | last post by:
I am a newbie to Access (most programming for that matter). I need help in creating an Access database that keeps the history of each user in a database. For example, a user might have lesson 1 sent...

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.