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 / Mailmerge and Fax / November 2003

Tip: Looking for answers? Try searching our database.

Merge to seperate specific named doc?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Greg - 25 Nov 2003 15:16 GMT
How can I do a mail merge that saves the results of each
merge to a different file? For example, I have an excel
partner sheet as my source document. I would like to have
each merged file saved as the corresponding partner name.
Therefore if I have 60 partners, I would have 60 word
documents, and I would like the filename to be the
partners name.
Peter Jamieson - 25 Nov 2003 15:37 GMT
You need a VBA macro to do this - if the merge is simple and only processes
one data source record per output document, you could use VBA to perform one
merge for each record in the data source, and generate the path/file name
for each document from the merge data.
Or you could do the entire merge to an output document and use a macro to
split the file using e.g. Section breaks to determine where to make the
split.

An example of code for the former approach would be:

Sub ProduceOneDocPerSourceRec()
'

' NB, needs bettor error management and doubtless other things a VBA expert
' will point out.

Dim intSourceRecord
Dim objMerge As Word.MailMerge
Dim strOutputDocumentName As String
Dim TerminateMerge As Boolean

' Need to set up this object as the ActiveDocument changes when the
' merge is performed. Besides, it's clearer.

Set objMerge = ActiveDocument.MailMerge
With objMerge

' If no data source has been defined, do it here using OpenDataSource.
' But if it is already defined in the document, you should not need to
define it here.

'  .OpenDataSource _
'    Name:="whatever"

 intSourceRecord = 1
 TerminateMerge = False

 Do Until TerminateMerge
   .DataSource.ActiveRecord = intSourceRecord

   ' if we have gone past the end (and possibly, if there are no records)
   ' then the Activerecord will not be what we have just tried to set it to

   If .DataSource.ActiveRecord <> intSourceRecord Then
     TerminateMerge = True
   ' the record exists
   Else

    ' while we are looking at the correct activerecord,
     ' create the document path name
     ' e.g. - you will need to change this -
     strOutputDocumentName = "c:\mydoc\" &
.DataSource.Datafields("Partner_Name").Value &
".doc"

     .DataSource.FirstRecord = intSourceRecord
     .DataSource.LastRecord = intSourceRecord
     .Destination = wdSendToNewDocument
     .Execute

     ' The Activedocument is always the output document
     ' Add any parameters you need to these calls
     ActiveDocument.SaveAs strOutputDocumentName
     ActiveDocument.Close
     intSourceRecord = intSourceRecord + 1
   End If
 Loop
End With
End Sub

If you need the other approach to splitting the file, there is code at
Graham Mayor's site for a couple of examples which you could adapt in a
similar way:
http://www.gmayor.dsl.pipex.com/individual_merge_letters.htm

If you need more on how to use these macros, see

http://www.mvps.org/word/FAQs/MacrosVBA/CreateAMacro.htm

--
Peter Jamieson - Word MVP

--
Peter Jamieson - Word MVP

> How can I do a mail merge that saves the results of each
> merge to a different file? For example, I have an excel
[quoted text clipped - 3 lines]
> documents, and I would like the filename to be the
> partners name.
- 25 Nov 2003 15:45 GMT
Thank you very much for your help. The macro works
perfectly.

>-----Original Message-----
>You need a VBA macro to do this - if the merge is simple and only processes
[quoted text clipped - 91 lines]
>
>.
 
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.