How to Use VBA to Generate Word Document From Template
🔍 WiseChecker

How to Use VBA to Generate Word Document From Template

You want to automate Word to create new documents from a template without manually opening, copying, and saving each time. VBA macros can open a template file, add content at specific bookmarks or content controls, and save the result as a new document. This article explains how to write a VBA macro that generates a Word document from a template, including how to reference the template, insert text, and handle file paths.

Key Takeaways: Automating Document Generation With VBA

  • Documents.Open method with the template path: Opens the .dotx or .dotm file as a new document instance.
  • Bookmarks or ContentControls object: Targets specific insertion points in the template for dynamic text replacement.
  • Document.SaveAs2 method: Saves the generated document as a .docx file in your chosen location.

How VBA Interacts With Word Templates

A Word template is a .dotx or .dotm file that contains styles, headers, footers, and placeholder content. When you open a template via VBA, Word creates a new document based on that template without altering the original file. This is the key mechanism for automated document generation.

VBA macros run inside Word, so you need to enable the Developer tab to access the Visual Basic Editor. The macro uses the Documents.Open method to load the template as a new document. After opening, you can insert text, replace bookmarks, or fill content controls with data from a database, Excel, or user input. Finally, the macro calls SaveAs2 to write the new file to disk.

Before writing the macro, create a template that includes bookmarks or content controls at every location where you want to insert dynamic content. For example, place a bookmark named ClientName where the client name should appear. The macro will locate that bookmark and replace it with the actual value.

Writing the VBA Macro to Generate a Document From a Template

This macro opens a template, fills in two bookmarks, and saves the result as a new document. You can adapt the code to use content controls or more complex logic.

  1. Open the Visual Basic Editor
    Press Alt+F11 in Word. In the Project Explorer, double-click ThisDocument or insert a new module from the Insert menu.
  2. Declare variables and define paths
    Write the following code at the top of the module:
    Sub GenerateFromTemplate()
       Dim wdApp As Word.Application
       Dim wdDoc As Word.Document
       Dim templatePath As String
       Dim savePath As String
       templatePath = "C:\Templates\InvoiceTemplate.dotx"
       savePath = "C:\Invoices\Invoice_" & Format(Now, "yyyymmdd_hhnnss") & ".docx"
  3. Open the template as a new document
    Add this line:
    Set wdDoc = Documents.Open(templatePath)
    This creates a new document based on the template.
  4. Insert text at a bookmark
    Replace the bookmark content with dynamic text:
    With wdDoc.Bookmarks("ClientName")
       .Range.Text = "Acme Corporation"
    End With

    Repeat for each bookmark in your template.
  5. Save the new document
    Add:
    wdDoc.SaveAs2 savePath
    wdDoc.Close
  6. Clean up object references
    End the macro with:
    Set wdDoc = Nothing
    Set wdApp = Nothing
    End Sub

Run the macro by pressing F5 while in the editor or by assigning it to a button on the Quick Access Toolbar. The new document appears in the save path you specified.

Common Issues When Generating Documents With VBA

The macro stops with “The item with the specified name was not found”

This error occurs when a bookmark or content control name in the code does not match the template. Open the template, press Ctrl+G to open the Immediate window, and type ActiveDocument.Bookmarks.ShowAll = True to display all bookmark brackets. Verify the exact spelling and case of each name. Content controls require using wdDoc.ContentControls instead of Bookmarks.

The template file path is not found

Word cannot locate the template if the path string is mistyped or the file is moved. Use an absolute path like C:\Templates\MyTemplate.dotx. For portability, store the path in a cell in an Excel workbook or in a Windows environment variable and read it with VBA.

The saved document still shows template content instead of the new values

This happens if the macro runs before the template fully loads. Insert a short delay after Documents.Open using Application.Wait (Now + TimeValue("0:00:01")). Alternatively, use DoEvents to force Word to process pending operations before inserting text.

VBA macro is disabled or blocked

Word blocks macros from untrusted locations by default. Save the .dotm file or the document containing the macro in a trusted folder. Go to File > Options > Trust Center > Trust Center Settings > Trusted Locations and add your project folder. Alternatively, digitally sign the macro project.

VBA Template Generation vs Manual Creation

Item VBA Macro Manual Process
Setup time 15–30 minutes to write and test the macro None
Speed per document Under 2 seconds 1–3 minutes depending on content
Error rate Near zero if bookmarks are correct Prone to typos and missed fields
Repeatability Identical output every run Varies with each operator
Skill required Basic VBA knowledge No programming needed

VBA automation is ideal when you generate the same document type frequently. Manual creation works for one-off documents where setup time is not justified.

You can now write a VBA macro that opens a Word template, fills in bookmarks or content controls, and saves a new document. Start by creating a template with named bookmarks for each field you need. Test the macro on a copy of the template to verify the output. For advanced use, integrate the macro with Excel data or a database to populate multiple fields in one run.