Mail merge with separate PDF record
Solved
Marie7831
Posted messages
12
Registration date
Status
Member
Last intervention
-
FRN -
FRN -
Hello everyone,
I'm desperately looking for a macro that can save each sheet of a mail merge as a separate PDF, using a mail merge field name (<<City>>) as the file name.
I've tried several macros but have never succeeded in getting what I need.
If anyone has an idea :)
Thank you in advance,
have a great day,
Marie
I'm desperately looking for a macro that can save each sheet of a mail merge as a separate PDF, using a mail merge field name (<<City>>) as the file name.
I've tried several macros but have never succeeded in getting what I need.
If anyone has an idea :)
Thank you in advance,
have a great day,
Marie
3 answers
yg_be
Posted messages
23437
Registration date
Status
Contributor
Last intervention
Ambassadeur
1 588
Hello, can you tell us more?
Just in case:
And in debug mode, this is what blocks first:
ActiveDocument.MailMerge.DataSource.ActiveRecord = wdLastRecord
Here is the macro:
Sub SavePubliAsPDF()
Dim LastRec As Integer
Dim Path As String, Id As String
Application.ScreenUpdating = False
'Choice of the folder for saving files
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Select a folder to save your files"
.Show
If Not (.SelectedItems.Count = 0) Then
Path = .SelectedItems(1)
Else
Exit Sub
End If
End With
'Counting the number of records in the mail merge
ActiveDocument.MailMerge.DataSource.ActiveRecord = wdLastRecord
LastRec = ActiveDocument.MailMerge.DataSource.ActiveRecord
ActiveDocument.MailMerge.DataSource.ActiveRecord = wdFirstRecord
'Saving the files
ActiveDocument.MailMerge.ViewMailMergeFieldCodes = False
For i = 1 To LastRec Step 1
Id = ActiveDocument.MailMerge.DataSource.DataFields(1).Value
ActiveDocument.SaveAs2 Path & "\Letter " & Id & ".pdf", wdFormatPDF
ActiveDocument.MailMerge.DataSource.ActiveRecord = wdNextRecord
Next i
MsgBox "The saving of your mail merge is complete." & vbLf & vbLf & LastRec & " files have been saved in the folder: " & Path, vbOKOnly + vbInformation, "Mail merge saving complete"
Application.ScreenUpdating = True
End Sub