logo
search
VBA & Macro Problems

How to Automatically Save a Word Document with a New Name from a Template via VBA

Algirdas JasaitisAlgirdas Jasaitis Oct 7, 2026 869 views

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.

How to Automatically Save a Word Document with a New Name from a Template
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA Editor

Press 'Alt + F11' to open the Visual Basic for Applications (VBA) editor.

2
Access ThisDocument Module

In the Project Explorer pane on the left, expand your template's project and double-click on 'ThisDocument'.

3
Add the Document_New Event

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.

4
Write the Save Logic

Inside the subroutine, write your save command using the ActiveDocument object. For example: ActiveDocument.SaveAs2 FileName:="C:\Your\Path\" & yourDynamicVariable & ".docx".

Use ActiveDocument and Document_New Event
Avoid Recursive Saves: Be cautious if you are using 'BeforeSave' events alongside automated saves, as this can trigger an endless loop. Temporarily disable application events during the save operation if necessary.
Powerful VBA Support

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. 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. 2. Launch the VBA Editor: Navigate to the 'Developer' tab and click on 'Visual Basic', or simply press Alt + F11.
  3. 3. Apply Your Template Macros: Insert your ActiveDocument logic into the 'ThisDocument' module just as you would in Microsoft VBA.
  4. 4. Save the Template: Save your document as a WPS Macro-Enabled Template (.wpt) or Word Macro-Enabled Template (.dotm) to preserve your code.
Full compatibility with Microsoft Word (.doc, .docx, .dotm) formats.Built-in VBA editor to easily create, edit, and run macros.Lightweight application that loads macro-enabled templates quickly.Familiar user interface ensuring a seamless transition.
QA img-9

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.