How to Use VBA to Apply a Word Template to Documents
🔍 WiseChecker

How to Use VBA to Apply a Word Template to Documents

You have a Word document that needs the styles, headers, or macros from a specific template. Manually attaching a template to each file takes time when you work with many documents. VBA automates this task by running a short macro that applies a template to one or all open documents. This article explains how to write and run a VBA macro to attach a Word template to a document. You will learn the exact code, how to modify it for your template path, and what to check if the macro does not work as expected.

Key Takeaways: Applying a Word Template with VBA

  • ActiveDocument.AttachedTemplate = “C:\path\template.dotx”: This single line of VBA code attaches a template to the currently active document.
  • For Each Doc In Application.Documents loop: Use this loop to apply a template to every open document at once instead of running the macro repeatedly.
  • Application.ScreenUpdating = False before the loop: Disable screen updates to speed up the macro when processing many files and prevent flickering.

What VBA Does When Attaching a Template to a Document

VBA is the programming language built into Word. When you attach a template using VBA, you change the AttachedTemplate property of a Document object. This property accepts a full file path to a .dotm, .dotx, or .dot file. The template supplies styles, AutoText entries, keyboard shortcuts, and macros to the document. The document itself does not move or change its formatting immediately unless you also run a separate command to update styles from the template.

Before you write the macro, you need two things. First, know the exact file path of the template. For example, C:\Users\YourName\AppData\Roaming\Microsoft\Templates\MyCompany.dotx. Second, enable the Developer tab in Word so you can open the VBA editor. Go to File > Options > Customize Ribbon and check the Developer box in the right column.

VBA macros are stored in a module inside the Normal.dotm template or inside the current document. For this task, store the macro in Normal.dotm so it is available for all documents.

Steps to Write and Run the VBA Macro to Attach a Template

  1. Open the VBA editor
    Press Alt + F11 on your keyboard. The Microsoft Visual Basic for Applications window opens. If you do not see the Project Explorer pane on the left, press Ctrl + R to show it.
  2. Insert a new module
    In the Project Explorer, right-click Normal and choose Insert > Module. A blank code window opens. This is where you paste the macro code.
  3. Paste the macro code
    Copy the following VBA code and paste it into the module window:

    Sub ApplyTemplateToActiveDoc()
    Dim templatePath As String
    templatePath = "C:\Users\YourName\AppData\Roaming\Microsoft\Templates\MyCompany.dotx"
    ActiveDocument.AttachedTemplate = templatePath
    MsgBox "Template applied to " & ActiveDocument.Name
    End Sub

    Replace the file path in the code with the actual path to your template file. Keep the double backslashes. For example, if your template is at D:\Templates\Letter.dotx, change the line to templatePath = "D:\Templates\Letter.dotx".

  4. Run the macro on the active document
    Close the VBA editor by clicking the X in the top-right corner. Back in Word, press Alt + F8 to open the Macros dialog. Select ApplyTemplateToActiveDoc from the list and click Run. Word attaches the template and shows a confirmation message.
  5. Optional: Update styles from the new template
    Attaching the template does not automatically update the document styles. To copy styles from the template into the document, add this line before the End Sub line:

    ActiveDocument.UpdateStyles
    This line replaces all styles in the document with the definitions from the attached template.

Apply the Template to All Open Documents at Once

  1. Create a new macro for multiple documents
    Open the VBA editor again (Alt + F11). Insert another module or add code below the existing macro. Paste this code:

    Sub ApplyTemplateToAllDocs()
    Dim doc As Document
    Dim templatePath As String
    templatePath = "C:\Users\YourName\AppData\Roaming\Microsoft\Templates\MyCompany.dotx"
    Application.ScreenUpdating = False
    For Each doc In Application.Documents
    doc.AttachedTemplate = templatePath
    doc.UpdateStyles
    Next doc
    Application.ScreenUpdating = True
    MsgBox "Template applied to all open documents"
    End Sub

  2. Run the macro
    Press Alt + F8, select ApplyTemplateToAllDocs, and click Run. The macro processes every open document. The screen updating is disabled during the loop to improve speed and reduce visual flicker.

What to Check If the Macro Does Not Work

The macro runs but nothing changes in the document

The most common cause is the missing ActiveDocument.UpdateStyles line. Without it, only the template link changes. The document still uses its old styles. Add the UpdateStyles line as shown in step 5 above.

Word shows an error: “Run-time error 4198”

This error means Word cannot find the template file at the path you provided. Double-check the path. Open File Explorer, navigate to the template location, and copy the exact path from the address bar. Paste it into the VBA code. Make sure the file extension matches: .dotm for macro-enabled templates, .dotx for template without macros, or .dot for older Word templates.

The macro works but styles update incorrectly

The UpdateStyles method replaces all styles in the document with those from the template. If the document had custom styles that do not exist in the template, those styles are removed. To keep existing styles, do not use UpdateStyles. Instead, manually update only specific styles by using the Organizer dialog or by copying styles one by one in VBA with the Copy method.

I want to apply the template to a document without opening it

You can open a document in the background, attach the template, save, and close it. Use the following code structure. Replace the file path with your document path and template path.

Sub ApplyTemplateToClosedDoc()
Dim doc As Document
Dim docPath As String
Dim templatePath As String
docPath = "C:\Docs\Report.docx"
templatePath = "C:\Templates\ReportTemplate.dotx"
Set doc = Documents.Open(docPath, Visible:=False)
doc.AttachedTemplate = templatePath
doc.UpdateStyles
doc.Save
doc.Close
End Sub

Item Active Document Only All Open Documents
VBA object ActiveDocument Application.Documents collection
Loop required No Yes, For Each doc In Application.Documents
Screen updating disabled Optional Recommended for speed
UpdateStyles needed Yes, if styles must change Yes, if styles must change
Risk of overwriting custom styles Same as any template change Same as any template change

You can now write and run a VBA macro to attach a template to one or many Word documents. Start with the single-document macro on a test file to verify the template path. Then use the multi-document loop when you need to process a batch of files. For advanced automation, combine this macro with a file dialog that lets you pick a template at runtime using the Application.FileDialog object.