Obtaining field value from active Mail Merge record

A

aiyou

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
 
H

Helmut Weber

Hi,

maybe an additional

..DataSource.ActiveRecord = i

will help.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
A

aiyou

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!
 

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