Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / July 2007

Tip: Looking for answers? Try searching our database.

Closing mail merge template

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Graham - 16 Jul 2007 11:10 GMT
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
Graham Mayor - 16 Jul 2007 12:03 GMT
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

Signature

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

My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

> Hi
>
[quoted text clipped - 19 lines]
> Regards
> GRaham
Graham - 16 Jul 2007 13:12 GMT
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

> The original is no longer the active document. You need to address the
> document you want to close. Something like:
[quoted text clipped - 26 lines]
> > Regards
> > GRaham
Ed - 16 Jul 2007 19:34 GMT
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
---------------------------------------------------------

Regards.

Ed

> Hi
>
[quoted text clipped - 37 lines]
> > > Regards
> > > GRaham
NZ VBA Developer - 16 Jul 2007 22:58 GMT
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!

> Hi Graham,
>
[quoted text clipped - 71 lines]
> > > > Regards
> > > > GRaham
Doug Robbins - Word MVP - 16 Jul 2007 20:33 GMT
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

Signature

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

> Hi
>
[quoted text clipped - 20 lines]
> Regards
> GRaham
Graham - 17 Jul 2007 11:18 GMT
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

> Dim mmmaindoc as Document
>     Set mmmaindoc = ActiveDocument
[quoted text clipped - 34 lines]
> > Regards
> > GRaham
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.