Closing a template from the autoOpen macro

D

Dixie

I have a mailmerge template with the following AutoOpen macro

ActiveDocument.MailMerge.OpenDataSource "C:\MergeData\LevelData.txt"
ActiveDocument.MailMerge.Execute

I want this template to automatically close when the mailmerge is executed
to the new document because I don't want to have to manually close two Word
documents after the mailmerge has happened.

Is there another line of code that can achieve that?

dixie
 
D

Doug Robbins - Word MVP

Dim maindoc as Document

Set maindoc = ActiveDocument
With maindoc
.MailMerge.OpenDataSource "C:\MergeData\LevelData.txt"
.MailMerge.Execute 'you may want to specify the destination
.Close wdDoNotSaveChanges
EndWith


--
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
 
P

Peter Jamieson

When you merge to a new document, it becomes the Activedocument. So you need
something more like

Dim objMainDocument As Word.Document
Set objMainDocument = ActiveDocument
objMainDocument.MailMerge.OpenDataSource "C:\MergeData\LevelData.txt"
objMainDocument.MailMerge.Execute
' if you want to save and close the new document, use something like
ActiveDocument.Saveas Filename:="whatever.doc"
ActiveDocument.Close Savechanges:=False
' To close the main document...
objMainDocument.Close

Peter Jamieson
 

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