logo
search
VBA & Macro Problems

How to Name Word Mail Merge Files Using Heading 1 Text with VBA

Adam DavisAdam Davis Oct 10, 2026 870 views

Question details

The user wants to modify a Word VBA macro to name split mail-merge files using the text of the Heading 1 paragraph in each document section, rather than generic numbers.

How to Name Word Mail Merge Files Using Heading 1 Text via VBA
Product
Word
Device & OS
not provided
Scenario
Splitting a mail-merged Word document into individual files and naming them dynamically based on the specific heading content of each section.
Observed behavior
The current macro outputs files named with generic sequential numbers, and the user requires the filenames to reflect the actual Heading 1 text found in each split document.
Before you start

Before modifying your macro, verify that each section of your mail-merged document consistently contains the target Heading 1 text as the first paragraph, and ensure the Developer tab is enabled to access the VBA editor.

Solution 1Recommended

Modify the VBA Macro to Extract and Sanitize Heading Text

Update your existing document-splitting macro to read the paragraph text, strip invalid characters, and save the file using the cleaned heading text.

When splitting a mail-merged document, the macro accesses each section individually. Instead of naming files with sequential numbers, the code can read the specific text range of the Heading 1 paragraph.

Because file names cannot contain characters like colons, slashes, or paragraph marks (carriage returns), you must use the VBA Replace function to sanitize the string before saving the document.

1
Open the Visual Basic Editor

Press Alt + F11 in Word to launch the VBA Editor, then locate the module containing your existing mail-merge splitting macro.

2
Declare the Title Variable

Add a new string variable to hold the heading text by inserting 'Dim sectionTitle As String' at the beginning of your procedure.

3
Extract the Heading Text

Inside your section loop, extract the text from the first paragraph (assuming it is the Heading 1) using: 'sectionTitle = Doc.Sections(x).Range.Paragraphs(1).Range.Text'.

4
Remove Invalid File Characters

Sanitize the text by removing the trailing paragraph mark and replacing invalid symbols. Add: 'sectionTitle = Replace(sectionTitle, vbCr, "")', followed by 'sectionTitle = Replace(sectionTitle, ":", "_")', 'sectionTitle = Replace(sectionTitle, "/", "_")', and 'sectionTitle = Replace(sectionTitle, "\", "_")'.

5
Save the Document with the New Name

Update your document save command to use the cleaned variable: 'ActiveDocument.SaveAs2 Doc.Path & "\" & sectionTitle & ".docx"'.

Modify the VBA Macro to Extract and Sanitize Heading Text
Clean Execution: By sanitizing the text, your macro will smoothly execute without throwing file naming errors caused by invalid characters or hidden paragraph breaks.
Advanced Macro Support

Easily Manage Mail Merges and Macros in WPS Office

WPS Office fully supports VBA macros, allowing you to run your mail merge and document-splitting scripts seamlessly. You can edit and execute the exact same VBA code in WPS Writer to dynamically name your merged files without formatting issues.

  1. 1. Open Your Document: Launch WPS Writer and open your completed mail-merged document.
  2. 2. Access the VBA Editor: Navigate to the Developer tab on the ribbon and click on the 'Visual Basic' icon to open the VBA Editor.
  3. 3. Insert the Macro Code: Insert a new Module, then paste your modified splitting and renaming macro that includes the text sanitization logic.
  4. 4. Run the Script: Press F5 or click the Run button to execute the script and automatically generate your cleanly named individual files.
Full support for VBA macros and advanced script automationSeamless format compatibility with Microsoft Word documents (.docx)Built-in, intuitive Mail Merge toolkit for bulk document creationLightweight application that processes heavy tasks efficiently
microsoft office alternative - wps office

Frequently Asked Questions

Why does my macro give a 'bad file name' error when saving the file?

This usually happens because the extracted Heading 1 text contains hidden paragraph marks (vbCr) or invalid filename characters like colons, slashes, or asterisks. You must use the Replace function in VBA to remove or replace these characters before calling the SaveAs2 command.

How do I handle duplicate Heading 1 names in my mail merge?

If multiple sections share the same Heading 1 text, saving them will cause overwriting. You can handle this by declaring an integer counter in your VBA loop and appending it to the filename (e.g., sectionTitle & "_" & counter & ".docx") if you detect a duplicate, or by appending a unique merge field value.

Can I extract text based on the Heading 1 style rather than the paragraph index?

Yes. Instead of blindly reading the first paragraph, you can use the Find object in VBA to search for text formatted specifically with the 'Heading 1' style within the current section's range. Once found, you can extract that exact range text to use as your filename.