Create Separate Two-Page PDFs from a Word Mail Merge Using VBA
Question details
Create a separate two-page PDF for each Word mail merge record using VBA, without requiring employees to install an add-in.

- Product
- Microsoft Word
- Device & OS
- not provided
- Scenario
- Employees need to run a mail merge and save each recipient's output as an individual PDF.
- Observed behavior
- The current mail merge exports all records into one PDF rather than separate files.
Open the Word mail merge main document and confirm that its data source contains a populated field named Username, with no unwanted empty rows. Check the recipient list and test a few records to confirm that each produces exactly two pages.
Merge each Word record to a separate PDF with VBA
Use this Windows Word macro to export records individually without installing an add-in.
The macro merges one record at a time, checks that the result has two pages, and exports it to PDF. Filenames include the Username value and a record number.
The macro stops if it finds a blank Username, an output that is not two pages, an existing filename, or a data source whose record count Word cannot determine. PDFs created before a stop remain in the selected folder.
In Word, open the mail merge main document and use Mailings > Select Recipients and Edit Recipient List to verify the connected source and recipients. Save the document as a macro-enabled Word document (.docm) if you want to store the macro in it.
Press Alt+F11, select the document's VBA project, and choose Insert > Module. Paste the following code into the module: Option Explicit Sub MergeRecordsToPDF() Dim mainDoc As Document, mergedDoc As Document Dim ds As MailMergeDataSource Dim folder As String, userName As String, pdfPath As String Dim i As Long, total As Long, oldFirst As Long, oldLast As Long Dim oldDestination As Long, savedSettings As Boolean Dim message As String, completed As Long On Error GoTo Failed Set mainDoc = ActiveDocument If mainDoc.MailMerge.MainDocumentType = wdNotAMergeDocument Then Err.Raise vbObjectError + 100, , "Open the mail merge main document first." End If Set ds = mainDoc.MailMerge.DataSource total = ds.RecordCount If total < 1 Then Err.Raise vbObjectError + 101, , "Word cannot determine a positive record count for this data source." End If With Application.FileDialog(msoFileDialogFolderPicker) .Title = "Choose a folder for the PDFs" If .Show <> -1 Then Exit Sub folder = .SelectedItems(1) End With oldFirst = ds.FirstRecord oldLast = ds.LastRecord oldDestination = mainDoc.MailMerge.Destination savedSettings = True For i = 1 To total ds.ActiveRecord = i userName = Trim$(ds.DataFields("Username").Value) If Len(userName) = 0 Then Err.Raise vbObjectError + 102, , "Username is blank for record " & i & "." End If userName = SafePDFName(userName) pdfPath = folder & "\" & userName & "_record_" & Format$(i, "0000") & ".pdf" If Len(Dir$(pdfPath)) > 0 Then Err.Raise vbObjectError + 103, , "File already exists: " & pdfPath End If ds.FirstRecord = i ds.LastRecord = i mainDoc.MailMerge.Destination = wdSendToNewDocument mainDoc.MailMerge.Execute Pause:=False Set mergedDoc = ActiveDocument mergedDoc.Repaginate If mergedDoc.ComputeStatistics(wdStatisticPages) <> 2 Then Err.Raise vbObjectError + 104, , "Record " & i & " did not produce exactly two pages." End If mergedDoc.ExportAsFixedFormat OutputFileName:=pdfPath, ExportFormat:=wdExportFormatPDF mergedDoc.Close SaveChanges:=wdDoNotSaveChanges Set mergedDoc = Nothing completed = completed + 1 Next i message = "Created " & completed & " PDF files in " & folder GoTo CleanUp Failed: message = "Stopped after creating " & completed & " PDF files. " & Err.Description CleanUp: On Error Resume Next If Not mergedDoc Is Nothing Then mergedDoc.Close SaveChanges:=wdDoNotSaveChanges If savedSettings Then ds.FirstRecord = oldFirst ds.LastRecord = oldLast mainDoc.MailMerge.Destination = oldDestination End If MsgBox message, vbInformation, "Mail merge PDF export" End Sub Private Function SafePDFName(ByVal value As String) As String Dim bad As Variant, item As Variant, n As Long bad = Array("\", "/", ":", "*", "?", Chr$(34), "<", ">", "|") For Each item In bad value = Replace$(value, CStr(item), "_") Next item For n = 0 To 31 value = Replace$(value, Chr$(n), "_") Next n SafePDFName = Left$(value, 80) End Function
Return to the mail merge main document, press Alt+F8, select MergeRecordsToPDF, and click Run. Choose an output folder when prompted.
Open the selected folder and verify the PDF count, filenames, recipients, and page counts. If the macro reports a problem, correct the source or document and use an empty output folder for the next run.

Work with Word Documents in WPS Office
WPS Office is a free, lightweight alternative with a familiar interface for opening and editing Word documents. Its format compatibility can make it easier to move everyday document work from Microsoft Office, although it does not fix Word VBA macro issues.

Frequently Asked Questions
Why does the macro stop when a merged record has more or fewer than two pages?
The macro checks the page count before exporting each record. Review that recipient's data and the main document's layout, page breaks, and margins, then run the export again.
Can I use a different field for the PDF filenames?
Yes. In the VBA module, replace Username in ds.DataFields("Username") with the exact name of a populated field in your connected data source.
Will the macro overwrite existing PDFs?
No. It stops if the generated filename already exists in the selected folder. Choose an empty folder or move the existing files before rerunning it.
What if Word cannot determine the record count?
The macro requires Word to report a positive DataSource.RecordCount. Check the connection through Mailings > Select Recipients, or try a supported local data source that Word can count.
Do employees need to install a mail merge add-in?
No. This solution uses Word's built-in mail merge and VBA features, but employees must be allowed to run the macro under their organization's security policy.




