Starting MSWord Merge from MSAccess

K

Kathy Webster

I am collecting info from an MSAccess record and trying to merge it in Word.
I would like this to work with any version of Word that the user may have
installed. My MSAccess app will be a runtime app that I will be putting on
various machines in different workplaces.
Here is the code I am running from MSAccess:

Public Function AddressBlock() As Boolean

Dim wd As Object
Dim wdActiveDoc As Object 'RM: Added this to help ensure that you're
'working on the document you intend to.
Dim wdField As Object 'RM: Note the change in name here to avoid
'ambiguity with the Field object in both Word and
'Access. As well, I changed the type, since
'(like Word.Application), you have to use late
'binding.

Set wd = CreateObject("Word.Application")
wd.Visible = True

wd.Application.ScreenUpdating = False

Set wdActiveDoc = wd.Documents.Open("K:\legalper\merge_ad.doc ", False,
False, _
False, "", "", False, "", "", 0)

With wdActiveDoc.MailMerge
.Destination = 0
.Execute
End With
wd.Windows("merge_ad.doc ").Activate
wd.ActiveWindow.Close 0
'It bombs here with "Automation error: The object invoked has disconnected
from its clients", and the next line is highlighted.
'What I'm trying to do here is open merge_ad.doc, merge it, close
merge_ad.doc, then save the
'resulting merged doc as "merged_ad.doc"
wdActiveDoc.saveas Filename:="K:\legalper\merged_ad.doc", FileFormat:=0

Thanks in advance,
Kathy
 
D

Doug Robbins - Word MVP

In place of

wd.Windows("merge_ad.doc ").Activate
wd.ActiveWindow.Close 0

use

wdActiveDoc.Close wdDoNotSaveChanges

Note, you do not show that you have declared wdActiveDoc as a document
object.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
K

Kathy Webster

I think I wasn't clear. It is successful up to and including :
wd.Windows("merge_ad.doc ").Activate
wd.ActiveWindow.Close 0

It bombs when executing this next line:

wdActiveDoc.saveas Filename:="K:\legalper\merged_ad.doc",
FileFormat:=0

....with the message "The object invoked has disconnected from its clients."
I don't know how to reactivate the screen that shows the RESULTS of the
merge to new document, and save THAT as "merged_ad.doc"
 
D

Doug Robbins - Word MVP

In place of

wd.Windows("merge_ad.doc ").Activate
wd.ActiveWindow.Close 0
wdActiveDoc.saveas Filename:="K:\legalper\merged_ad.doc"

use

wdActiveDoc.Close wdDoNotSaveChanges
wd.ActiveDocument.SaveAs "K:\legalper\merged_ad.doc"

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
K

Kathy Webster

Cool, thanks. I had make 1 change to this line:

wdActiveDoc.Close wdDoNotSaveChanges

to

wdActiveDoc.Close , 0

because it didn't understand wdDoNotSaveChanges

One last thing: Now my Word screen is left with "merged_ad.doc" in the
title bar, and the screen is inactive (gray background, no white "sheet of
paper" ready to go). How can I leave Word with no document in the title bar
and a white "Sheet of paper" on the screen?
 
D

Doug Robbins - Word MVP

Earlier in your code, you have turned off ScreenUpdating

Try adding the following at the end.

wd.ScreenUpdating = True
wd.ScreenRefresh
wd.ActiveDocument.Close
wd.Documents.Add

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
K

Kathy Webster

You da bomb...Thanks!
One last question...I hope...
How do I modify this line you gave me, so that "merged_ad.doc" doesn't show
up on the recently edited docs list?

wd.ActiveDocument.SaveAs "K:\legalper\merged_ad.doc"
 
D

Doug Robbins - Word MVP

Use

wd.ActiveDocument.SaveAs "K:\legalper\merged_ad.doc", SaveToRecentFiles:
= False

or, as you seem to have had troubled with using named parameters, you may
need to use

wd.ActiveDocument.SaveAs "K:\legalper\merged_ad.doc","","","", False



--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
K

Kathy Webster

Unfortunately, it didn't like either of those, but I messed with it, and it
likes:
wd.ActiveDocument.SaveAs "K:\legalper\merged_ad.doc", _
0, False, "", False, "", False, False, False, False, False

Thank you!
 
K

Kathy Webster

On a related note,
Now that I'm starting to work with this other similar part of the code
(except this is opening the doc, rather than saving as...), I have found
that even though I think I have specified to leave merge_ad.doc off of the
Recently edited files list, it's still showing up:

Public Function AddressBlock() As Boolean

Dim wd As Object
Dim wdActiveDoc As Object 'RM: Added this to help ensure that you're
'working on the document you intend to.
Dim wdField As Object 'RM: Note the change in name here to avoid
'ambiguity with the Field object in both Word and
'Access. As well, I changed the type, since
'(like Word.Application), you have to use late
'binding.

Set wd = CreateObject("Word.Application")
wd.Visible = True

wd.Application.ScreenUpdating = False

Set wdActiveDoc = wd.Documents.Open("K:\legalper\merge_ad.doc ", False,
False, False, "", "", False, "", "", 0)

Can anyone figure out why?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top