Closing mail merge template

G

Graham

Hi

I have some code which opens a Word template then runs a mail merge.

With ActiveDocument.MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute Pause:=True
End With

What I want to do is now close the word template

If I use ActiveDocument.Close then the newly created mail merge document is
closed not the original word template.

Any help greatly appreciated.

Regards
GRaham
 
G

Graham Mayor

The original is no longer the active document. You need to address the
document you want to close. Something like:

Windows("C:\Path\Docname.doc").Close SaveChanges:=wdDoNotSaveChanges

should do the trick


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
G

Graham

Hi

I tried that but I get a Run Time error 5941

The requested member of the collection does not exist.

I am using Word 2007 but soem machines use 2003

Regards
Graham
 
E

Ed

Hi Graham,

I think an alternative approach is to set an explicit reference to the mail
merge main document (e.g. when you are opening or creating it or at some
later point)
and then work with it via the object variable E.g. ...

---------------------------------------------------------
Sub Test()
Dim MMMDoc As Document

Set MMMDoc = Documents.Open("c:\temp\TestDoc.doc")

With MMMDoc.MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute Pause:=True
End With

MMMDoc.Close SaveChanges:=wdDoNotSaveChanges

Set MMMDoc = Nothing

End Sub
 
D

Doug Robbins - Word MVP

Dim mmmaindoc as Document
Set mmmaindoc = ActiveDocument
With mmmaindoc.MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute Pause:=True
.Close wdDoNotSaveChanges 'or wdSaveChanges if you want to save any
changes to the main document
End With


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

NZ VBA Developer

G'day Graham,

I faced a similar problem with code to email a document as an attachment.
The original code (which I inherited) simply closed the "active" document,
which worked fine unless the user was using Word to edit messages in Outlook.
In that instance, the new mail message became the active document, and the
code closed the message rather than the document that was intended to be used
simply as an attachment and then discarded.

To get around this problem I modified the code to make specific reference to
the attachment document, as follows:

Private Sub cmdEMail_Click()
~OMITTED: code to create variables, hide the userform (to prevent
Outlook/Word from throwing a wobbly about "open dialog boxes") and password
protect the document~
'*** Save the doc in the temp location ***
MyFile = Environ("Temp") & "\MYFILE.doc"
ActiveDocument.SaveAs MyFile
~OMITTED: code to do the Outlook stuff to create the mail message and attach
the temp file~
'*** Close and kill the temp file using a specific reference to the file
rather than just ActiveDocument ***
Documents(MyFile).Close (wdDoNotSaveChanges)
Kill MyFile
~ OMITTED: code to display the mail message and unload the userform~
End Sub

I expect something similar would work for closing your mailmerge template.
Just another take on what Ed said.

Cheers!
 
G

Graham

Hi Doug

I tried your code but got an error at the .Close part so I used ED's code
and it all worked fine.

Thanks to everyone for their help.

Regards
Graham
 

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