Hi, all, I need to run a mail merge that results in individual documents being saved. Ideally, when the merge takes place the name of each document could come from a field in the data set. Any suggestions for how to achieve this?
Thanks!
Hi, all, I need to run a mail merge that results in individual documents being saved. Ideally, when the merge takes place the name of each document could come from a field in the data set. Any suggestions for how to achieve this?
Thanks!
Re: Mail Merge output to named individual files
Figured this out. Here's the code I'm using, which is a little sloppy and inelegant in particular when it comes to the total count of records for the for/next loop. I'm manually entering that in the mail merge data file. If anyone has a suggestion on how to count the number of records and just use that, I'd love to incorporate it.
Dim FileLocation As String 'Field must be derived from data source
Dim DocName As String 'Field must be derived from data source
Dim Count As Integer 'Field must be derived from data source
Count = ActiveDocument.MailMerge.DataSource.DataFields("Count").Value 'how many docs in merge?
ActiveDocument.MailMerge.DataSource.ActiveRecord = wdFirstRecord 'Make sure you start at the beginning!
For i = 1 To Count
With ActiveDocument.MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = ActiveDocument.MailMerge.DataSource.ActiveRecord
.LastRecord = ActiveDocument.MailMerge.DataSource.ActiveRecord
'Fill in variables:
FileLocation = .DataFields("FileLocation").Value 'added code
DocName = .DataFields("DocName").Value 'added code
End With
'Merge the active record:
.Execute Pause:=False
End With
'Save the resulting document. Note modified Filename!
ActiveDocument.ExportAsFixedFormat OutputFileName:= _
FileLocation + DocName + ".pdf", _
ExportFormat:=wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
wdExportOptimizeForPrint, Range:=wdExportAllDocument, From:=1, To:=1, _
Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
BitmapMissingFonts:=True, UseISO19005_1:=False
'Close the resulting merge document, don't save changes
ActiveWindow.Close savechanges:=False
'Back to the original template document, advance to next record before doing it all again
ActiveDocument.MailMerge.DataSource.ActiveRecord = wdNextRecord
Next i
Display More
Don’t have an account yet? Register yourself now and be a part of our community!