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