How to Save Word Documents Using the Title with VBA
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.

- 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.
Ensure that your Word document has the Developer tab enabled and that you have saved your current work before testing any new VBA macros.
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.
Press Alt + F11 on your keyboard to open the Visual Basic for Applications (VBA) Editor.
Insert a new module and declare your range variable by typing: Dim rngTitle As Range
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
Reduce the end of the range by one character to exclude the paragraph mark: rngTitle.End = rngTitle.End - 1
Execute the save command using the cleaned text as the filename: ActiveDocument.SaveAs rngTitle.Text & ".doc"

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. Open WPS Writer: Launch WPS Office and open your text document in WPS Writer.
- 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. Run Your Script: Paste your customized VBA code for saving the document by title, and click the 'Run' button to execute the macro.

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.




