VBA & Macro Problems

How to Save Word Documents Using the Title with VBA

Amos GikundaAmos Gikunda Oct 23, 2026 869 views

Question details

The user needs to save multiple Word documents using their titles as file names via VBA, but without including the paragraph mark in the file name.

How to Save Word Documents Using the Title with VBA
Product
Word
Device & OS
not provided
Scenario
Automating the saving process of multiple Word documents using VBA scripts by extracting the title text.
Observed behavior
The user wants to extract the document title using a VBA Range object and use it as the file name while omitting the trailing paragraph mark.
Before you start

Ensure that your Word document has the Developer tab enabled and that you have saved your current work before testing any new VBA macros.

Solution 1Recommended

Use a VBA Range Object to Exclude the Paragraph Mark

Extract the title text and manually reduce the range endpoint by one character to remove the paragraph mark before saving.

When you extract a line of text in Word using VBA, the Range object often includes the hidden paragraph mark at the end. Including this mark in a file name causes a run-time error because it is an invalid character for file paths. To fix this, you can adjust the Range object's end position by subtracting one character.

1
Open the VBA Editor

Press Alt + F11 on your keyboard to open the Visual Basic for Applications (VBA) Editor.

2
Define the Range Variable

Insert a new module and declare your range variable by typing: Dim rngTitle As Range

3
Set the Range to the Document Title

Assign the specific text line you want to use as the title. For example, to use the current line: Set rngTitle = ActiveDocument.Bookmarks("\line").Range

4
Remove the Paragraph Mark

Reduce the end of the range by one character to exclude the paragraph mark: rngTitle.End = rngTitle.End - 1

5
Save the Document

Execute the save command using the cleaned text as the filename: ActiveDocument.SaveAs rngTitle.Text & ".doc"

Use a VBA Range Object to Exclude the Paragraph Mark
Macro Success: By reducing the range end by 1, the script successfully removes the hidden paragraph symbol, ensuring the filename is valid and the document saves correctly.

Automate Document Management in WPS Office

WPS Office provides excellent support for VBA macros, allowing you to seamlessly automate repetitive tasks like saving documents by their titles.

  1. 1. Open WPS Writer: Launch WPS Office and open your text document in WPS Writer.
  2. 2. Access the Macro Editor: Navigate to the Developer tab on the top ribbon and click on 'Macros', or press Alt+F11 to open the VBA Editor.
  3. 3. Run Your Script: Paste your customized VBA code for saving the document by title, and click the 'Run' button to execute the macro.
Highly compatible with Microsoft Word VBA macros and scripts.Fully supports standard Word file formats like .doc and .docx.Lightweight environment for fast execution of VBA automation.Familiar UI that makes navigating the Developer tab easy.
QA img-9

Frequently Asked Questions

Why does my VBA script throw an error when saving with a title?

This usually happens because the extracted title text contains illegal characters for filenames, such as a paragraph mark or line break. Removing the last character of the text range resolves this issue.

How do I enable the Developer tab to run VBA macros?

Go to File > Options > Customize Ribbon, and check the box next to 'Developer'. This will add the Developer tab to your main ribbon, giving you access to the Macro editor.

Can I save the document as a .docx file instead of .doc?

Yes, simply change the file extension in your save command. For newer formats, use: ActiveDocument.SaveAs2 FileName:=rngTitle.Text & ".docx", FileFormat:=wdFormatXMLDocument.