logo
search
VBA & Macro Problems

How to Check Active Document Template Using Word VBA Macro

Emma BrownEmma Brown Oct 7, 2026 869 views

Question details

The user wants to write a VBA macro that checks if the currently active document is attached to a specific template and displays a message box if it matches.

How to Check the Active Document Template Using Word VBA
Product
Word
Device & OS
not provided
Scenario
Developing a macro that conditionally executes code based on the specific template file attached to the active document.
Observed behavior
The user needs to programmatically retrieve the template name attached to the active document and compare it to a target string in VBA.
Before you start

Ensure that the Developer tab is enabled in your word processor to access the VBA editor. It is also recommended to save your document as a Macro-Enabled Document (.docm) to retain your scripts.

Solution 1Recommended

Use the Document.AttachedTemplate Property in VBA

Access the VBA editor and use the AttachedTemplate property to evaluate the template name of the active document and trigger a message box upon a match.

The Document.AttachedTemplate property returns a Template object representing the template attached to the specified document. By comparing its name as a string, you can execute targeted commands like displaying a message box when a specific template is active.

1
Open the VBA Editor

Press Alt + F11 on your keyboard to open the Visual Basic for Applications (VBA) editor in your word processor.

2
Insert a New Module

In the Project Explorer window on the left, right-click on your document project, select 'Insert', and choose 'Module'.

3
Enter the VBA Code

Copy and paste the following code snippet into the module window: Sub CheckTemplate() If ActiveDocument.AttachedTemplate = "Header document.dotx" Then MsgBox "Hi" End If End Sub

4
Run the Macro

Press F5 or click the 'Run' button on the toolbar to execute the macro. If the attached template matches 'Header document.dotx', a message box will appear.

Use the Document.AttachedTemplate Property in VBA
Template Name Matching: Ensure the template name in your script exactly matches the file name, including the extension (e.g., .dotx or .dotm), as the text comparison is precise.
Powerful Macro Support in WPS Office

Create and Run VBA Macros Easily in WPS Writer

WPS Office provides robust support for VBA macros, allowing you to automate repetitive tasks and manage document templates just like in Microsoft Word.

  1. 1. Download and Install WPS Office: Get the free WPS Office suite from the official website and complete the installation process.
  2. 2. Enable Developer Tools: Open WPS Writer, navigate to the settings menu, and ensure the Developer tab is visible on your ribbon.
  3. 3. Open the Macro Editor: Click on the Developer tab and select 'Visual Basic' or press Alt+F11 to start scripting your macros.
  4. 4. Run Your Code: Paste your ActiveDocument.AttachedTemplate validation script and run it seamlessly within the WPS VBA environment.
Highly compatible with Microsoft Word VBA scripts and .docm/.dotm formats.Lightweight application with fast macro execution speeds.Built-in VBA editor for seamless script writing, debugging, and running.Free alternative with a highly familiar user interface for seamless migration.
microsoft office alternative - wps office

Frequently Asked Questions

How do I find out what template is attached to my Word document without using VBA?

You can manually check the attached template by going to the Developer tab, clicking on 'Document Template', and looking at the 'Document template' field in the Templates and Add-ins dialog box.

Why does ActiveDocument.AttachedTemplate return Normal.dotm?

'Normal.dotm' is the default global template for all new Word documents. If you haven't explicitly attached a custom template to your document, this property will always return the default Normal template.

Can I use VBA to change the attached document template?

Yes, you can change the attached template by assigning a new valid file path to the property. For example: ActiveDocument.AttachedTemplate = "C:\Path\To\NewTemplate.dotx".

Are macros written in Microsoft Word compatible with WPS Office?

Yes, WPS Office versions with VBA support provide high compatibility with Microsoft Office macros. You can run most Word VBA scripts, including document template validations, with little to no modification.