Create multiple documents using Mail Merge (with File Names picked from Excel)

HAVISH MADHVAPATY
3 min readDec 25, 2021

Step-by-Step Mail Merge in Word works very well.
It can create separate documents for you.

Let us we are creating a letter for 4 employees, with their Name, Performance and Potential Scores.

But these are our two additional requirements:

  1. A separate word document should be created for each Employee
  2. The file name should be the Employee Name

This is the data:

We first create a mail merge document:

The /// here is the text that will be used to split documents into multiple documents using Word Macro.

After you create the Mail Merge letter, Edit Individual Documents.

Save the file in an empty Folder as a Macro Enabled (.docm) file.

Use the following code:

Sub SplitNotes(delim As String, strFilename As String)
Dim doc As Document
Dim arrNotes
Dim I As Long
Dim X As Long
Dim Response As Integer
arrNotes = Split(ActiveDocument.Range, delim)
Response = MsgBox("This will split the document into " & UBound(arrNotes) + 1 & " sections.Do you wish To proceed?", 4)
If Response = 7 Then Exit Sub
For I = LBound(arrNotes) To UBound(arrNotes)
If Trim(arrNotes(I)) <> "" Then
X = X + 1
Set doc = Documents.Add
doc.Range = arrNotes(I)
doc.SaveAs ThisDocument.Path & "\" & strFilename & Format(X, "000")
doc.Close TRUE
End If
Next I
End Sub
Sub test()
'delimiter & filename
SplitNotes "///", "File "
End Sub

This will create multiple documents in the same Folder.

Shift the files (except the .docm file into a new Folder)

Run the Excel Macro. Old names are in Column A, New Names are in Column B, Folder Path is in D2.

'This method renames all the filenames in a folder
Sub RenameAllFilenamesInAFolder()
Dim intRowCount As Integer
Dim intCtr As Integer
Dim strFileNameExisting As String
Dim strFileNameNew As String
Dim strFolder As String

'Set the folder path
strFolder = Range("D2").Value

With Sheets("SheetRename")
'Find the total rows count in the sheet
'This will be the last non-blank cell in column A...
intRowCount = .Cells(.Rows.Count, "A").End(xlUp).Row

'Loop through from the 2nd row (1st row is Heading)
'till the total rows in the sheet
For intCtr = 2 To intRowCount
'Get the existing filename from the cell
strFileNameExisting = .Range("A" & intCtr)

'Get the new filename from the cell
strFileNameNew = .Range("B" & intCtr)

'Rename the file
Name strFolder & strFileNameExisting As strFolder & strFileNameNew
Next intCtr
End With

'Display an appropriate message, once complete
MsgBox "All files renamed successfully!", _
vbInformation, "All files renamed"
End Sub

Names are changed

You can find the video here:
https://youtu.be/q-RqYDvkNgA

You can find the codes and files on GitHub here:
https://github.com/havishmad/word_mail_merge_automation

--

--

HAVISH MADHVAPATY

Founder @ Havish M Consulting | 40u40 [Analytics Insight] | AuthorX20 | MOS | MCDA | MCT | Taught at IIM ABCLK