How to Automatically Save a Word Document with a New Name from a Template via VBA
Question details
The user wants to use Word VBA events to automatically save a new document generated from a template using a custom filename and store it in a specific directory.

- Product
- Word Processing / VBA
- Device & OS
- not provided
- Scenario
- Automating the file generation and saving process when creating a new document from a standardized template.
- Observed behavior
- The user needs to correctly reference the new document instead of the template in VBA, applying user-entered information to generate the new filename.
Ensure you have Developer tools and macros enabled in your Word processor, and always test VBA scripts in a dedicated test folder to prevent accidental overwriting of existing files.
Use ActiveDocument and Document_New Event
Correctly target the newly created document using the ActiveDocument object and trigger the automated save via the Document_New event.
When writing VBA code inside a template, referencing 'ThisDocument' targets the template file itself. To manipulate the newly generated file, you must use 'ActiveDocument'. You can utilize the 'Document_New' event to run the automated saving process as soon as the file is created.
Press 'Alt + F11' to open the Visual Basic for Applications (VBA) editor.
In the Project Explorer pane on the left, expand your template's project and double-click on 'ThisDocument'.
From the dropdown menus at the top of the code window, select 'Document' on the left and 'New' on the right to insert a Private Sub Document_New() subroutine.
Inside the subroutine, write your save command using the ActiveDocument object. For example: ActiveDocument.SaveAs2 FileName:="C:\Your\Path\" & yourDynamicVariable & ".docx".

Set a Default Name Without Auto-Saving
If you prefer the user to manually click save but want to provide a pre-filled filename, you can modify the document's built-in Title property.
Automate Document Workflows with WPS Office
WPS Office provides robust support for VBA and macros, allowing you to automate repetitive tasks like saving templates with dynamic names exactly as you would in Microsoft Word.
- 1. Enable Developer Mode in WPS Writer: Go to 'Options' > 'Customize Ribbon', and check the 'Developer' tab to reveal Macro and VBA tools on your ribbon.
- 2. Launch the VBA Editor: Navigate to the 'Developer' tab and click on 'Visual Basic', or simply press Alt + F11.
- 3. Apply Your Template Macros: Insert your ActiveDocument logic into the 'ThisDocument' module just as you would in Microsoft VBA.
- 4. Save the Template: Save your document as a WPS Macro-Enabled Template (.wpt) or Word Macro-Enabled Template (.dotm) to preserve your code.

Frequently Asked Questions
Why does my VBA script overwrite the original template instead of the new document?
This occurs when your macro uses 'ThisDocument.SaveAs'. In template programming, 'ThisDocument' refers to the template containing the macro. To save the newly generated file, you must use 'ActiveDocument.SaveAs'.
Can I pull user information from a UserForm to generate the filename?
Yes. You can program a UserForm to display during the 'Document_New' event, collect the user's input, assign it to a string variable, and use that variable in your 'ActiveDocument.SaveAs2' file path.
Does WPS Office support Word VBA macros?
Yes, the professional versions of WPS Office have excellent support for VBA macros, utilizing an environment highly compatible with standard Microsoft Office VBA scripts.




