I know there has been a number of inquiries regarding using a value from a
Mail Merge record to dynamically name an output file. My objective is to
create 'x' number of documents using the same template, but save each on
using a distinct filename from a value within each record.
I've come close by referencing the
ActiveDocument.MailMerge.DataSource.DataFields(i).Value
This works successfully for the first record. However, subsequent records
retain the value from the first record, essentially overwritting the original
file. Any ideas?
I've tried various approaches, so there may be a few obsolete/irrevelant
variables
Sample below:
Sub mcrIndividualMailMerge
'
' mcrIndividualMailMerge Macro
' Macro recorded 02/16/2007
'
Application.DisplayAlerts = wdAlertsNone
Application.Visible = True
Dim StartRecord
Dim EndRecord
Dim i
Dim tmpFilename As String
Dim MailMergeApp As Object
Set MailMergeApp = Word.Application
Dim Doc As Document
Set Doc = ActiveDocument
StartRecord = InputBox("StartRecord")
EndRecord = InputBox("EndRecord")
For i = StartRecord To EndRecord
With ActiveDocument.MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = i
.LastRecord = i
End With
With Doc.MailMerge.DataSource
tmpFilename = .DataFields(1).Value
End With
.Execute Pause:=False
End With
ActiveDocument.SaveAs FileName:= _
"c:\adobework\tmp\" & tmpFilename, FileFormat:=wdFormatText _
, LockComments:=False, Password:="", AddToRecentFiles:=True, _
WritePassword:="", ReadOnlyRecommended:=False,
EmbedTrueTypeFonts:=False, _
SaveNativePictureFormat:=False, SaveFormsData:=False,
SaveAsAOCELetter:= _
False, Encoding:=1252, InsertLineBreaks:=False,
AllowSubstitutions:=False _
, LineEnding:=wdCRLF
ActiveDocument.Close
Next
End Sub
Helmut Weber - 16 Feb 2007 19:01 GMT
Hi,
maybe an additional
.DataSource.ActiveRecord = i
will help.

Signature
Greetings from Bavaria, Germany
Helmut Weber, MVP WordVBA
Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
aiyou - 16 Feb 2007 20:03 GMT
Helmut, you are a life saver!
I had to break it into two sections..the first to set the current record,
and then the second on to actually retrieve the values...otherwise, while my
filename was changing, the data actually merged remained that of the first
record.
With .DataSource
.ActiveRecord = i
tmpFilename = .DataFields(1).Value
End With
With .DataSource
.FirstRecord = i
.LastRecord = i
End With
There might be more efficient ways to deal with this, but for now, this
solves a BUNCH of problems.
Danke!
> Hi,
>
[quoted text clipped - 3 lines]
>
> will help.