Word SaveAs from Access problem

P

Phill

I am trying to create a new mailmerge document from Access, close the
template, and save the file so that I can email it. The problem is after I
close the template I don't have a reference to the new document created.
This is my code:

'-- Create the OLE instance of Word, then activate it.
Set WordApp = CreateObject("Word.Application")

Set WordDoc = WordApp.Documents.Add(Template:= _
Path & WordTemplate, _
NewTemplate:=False)

'Set WordDoc = GetObject(strFinalDoc, "Word.Document")
WordDoc.Application.Visible = True

WordDoc.MailMerge.OpenDataSource _
Name:=CurrentDb.Name, _
LinkToSource:=True, _
ReadOnly:=True, _
SQLStatement:="SELECT * FROM qryRptROW", _
subtype:=wdMergeSubTypeWord2000

WordDoc.MailMerge.Execute
WordDoc.Close False


If Dir("C:\Right-of-Way-Request.doc") <> "" Then
Kill "C:\Right-of-Way-Request.doc"
End If

WordDoc.ActiveWindow.Activate
WordDoc.SaveAs "C:\Right-of-Way-Request.doc"

Set WordDoc = Nothing
 
C

Cindy M -WordMVP-

Hi =?Utf-8?B?UGhpbGw=?=,
I am trying to create a new mailmerge document from Access, close the
template, and save the file so that I can email it. The problem is after I
close the template I don't have a reference to the new document created.
I had to read this through a couple of times, before I picked up what you
meant because you "misuse" the word terminology. For clarity when dealing with
"Wordies":

a template is a *.dot file and is used via File/New in the UI or with the
Documents.Add method when automating. You should never OPEN a template except
when creating or editing it.

the document in which mail merge fields are inserted is the mail merge main
document.

Now that that's cleared up, you are correct that the object model doesn't give
us a good, clean way to pick up the merge result. If you have Word 2002 or
2003 you can use the new merge events to do that (MailMergeAfterMerge provides
a resultDoc object). Other than that, you have to rely on the ActiveDocument
being the result. Since you use a new instance of Word, and a document object
for the main merge document, this shouldn't be a problem. Just do
WordDoc.Close, then Set REsultDoc = WordApp.ActiveDocument should give you the
merge result.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 

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