Word.Tips.Net WordTips (Menu Interface)

Batch Template Changes

Please Note: This article is written for users of the following Microsoft Word versions: 97, 2000, 2002, and 2003. If you are using a later version (Word 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Word, click here: Batch Template Changes.

Templates are used to quickly define a standard look for a document or a group of documents. The wonderful thing about templates is that you can define one template to give your document one look, and another template to give it an entirely different look. All you need to do, of course, is change which template is associated with a document.

Changing the template associated with one or two documents is quite easy. What if you have a directory full of documents whose templates need to be changed? This can get quite tedious very quickly. This is where a macro can come to the rescue—to relieve that old tedium and do the mundane very quickly. The following macro, ChangeTemplates, modifies all the documents in a particular directory to make sure they use the template you want.

Sub ChangeTemplates()
    Dim strDocPath As String
    Dim strTemplateB As String
    Dim strCurDoc As String
    Dim docCurDoc As Document

    ' set document folder path and template strings
    strDocPath = "C:\path to document folder\"
    strTemplateB = "C:\path to template\templateB.dot"

    ' get first doc - only time need to provide file spec
    strCurDoc = Dir(strDocPath & "*.doc")

    ' ready to loop (for as long as file found)
    Do While strCurDoc <> ""
        ' open file
        Set docCurDoc = Documents.Open(FileName:=strDocPath & strCurDoc)
        ' change the template
        docCurDoc.AttachedTemplate = strTemplateB
        ' save and close
        docCurDoc.Close wdSaveChanges
        ' get next file name
        strCurDoc = Dir
    MsgBox "Finished"
End Sub

In order to use the macro, simply make sure that the strDocPath and strTemplateB variables are set properly. The macro changes the template associations for all documents in a particular directory. If you want something a little more discriminating, then a different macro is in order. For instance, you may want to have the macro examine each document and only change those that use TemplateA so that they now use TemplateB. In this instance you will find the following macro very handy:

Sub TemplateBatchChange()
    Dim objPropertyReader
    Dim strFolder As String
    Dim strFileName As String
    Dim objThisDoc As Word.Document
    Dim strFindTemplate As String
    Dim strReplaceTemplate As String
    Dim strAffectedDocs As String

    On Error Resume Next

    'Create the PropertyReader object
    Set objPropertyReader = CreateObject("DSOleFile.PropertyReader")
    If Err.Number <> 0 Then
        MsgBox "You must install the DSOleFile component. See " & _
        GoTo FinishUp
    End If

    'Get the template names
    strFindTemplate = UCase(InputBox("Name of template to find (exclude the .dot)") & _

    strReplaceTemplate = InputBox("Name of replacement template (exclude the .dot)") & _

    'Make sure it's a valid template. Try to create a new document based on it.
    Set objThisDoc = Word.Documents.Add(strReplaceTemplate, Visible:=False)
    If Err.Number <> 0 Then
        'No such template
        MsgBox "There is no accessible template named " & strReplaceTemplate
        GoTo FinishUp
    End If
    'Close the test document
    objThisDoc.Close wdDoNotSaveChanges

    On Error GoTo ErrorHandler
    'Get the current documents path
    strFolder = Word.Application.Options.DefaultFilePath(wdDocumentsPath) _
      & Word.Application.PathSeparator

    'Examine all Word documents in the directory

    'Get the first document name
    strFileName = Dir(strFolder & "*.doc")

    While strFileName <> ""
        'Look at the template name
        If UCase(objPropertyReader.GetDocumentProperties _
          (strFolder & strFileName).Template) = strFindTemplate Then

            'It matches. Open the document
            Set objThisDoc = Word.Documents.Open _
              (FileName:=strFileName, Visible:=False)

            'Change the attached template
            objThisDoc.AttachedTemplate = strReplaceTemplate

            'Save the change
            objThisDoc.Close wdSaveChanges

            'Note the document
            strAffectedDocs = strAffectedDocs & strFileName & ", "
        End If
        'Get the next document
        strFileName = Dir

    'Report the results
    If strAffectedDocs = "" Then
        MsgBox "No documents were changed.", , "Template Batch Change"
        'Remove the trailing comma and space
        strAffectedDocs = Left(strAffectedDocs, Len(strAffectedDocs) - 2)

        MsgBox "These documents were changed: " & _
          strAffectedDocs, , "Template Batch Change"
    End If
    GoTo FinishUp

    Set objThisDoc = Nothing
    Set objPropertyReader = Nothing
    Err.Raise vbError + 1001, "TemplateBatchChange", _
      "TemplateBatchChange encountered an error: " & Err.Description

    'Release object references
    Set objThisDoc = Nothing
    Set objPropertyReader = Nothing
End Sub

In order to use this macro, you must first make sure that the DSOLEFILE component (available free from Microsoft) is installed on your system. You can find out more about this component by referring to Microsoft's 224351 Knowledge Base article. The macro provides you a chance to specify a template name to be replaced and the name of the template to replace it with. It even checks to make sure that the replacement template exists.

This macro searches for documents in the default Word document folder. If you want to search in a different directly, you should make sure that the strFolder variable gets set to the full path of the folder you want used.

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (1437) applies to Microsoft Word 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Word (Word 2007 and later) here: Batch Template Changes.

Related Tips:

The First and Last Word on Word! Bestselling For Dummies author Dan Gookin puts his usual fun and friendly candor back to work to show you how to navigate Word 2013. Spend more time working and less time trying to figure it all out! Check out Word 2013 For Dummies today!


Leave your own comment:

  Notify me about new comments ONLY FOR THIS TIP
Notify me about new comments ANYWHERE ON THIS SITE
Hide my email address
*What is 5+3 (To prevent automated submissions and spam.)
           Commenting Terms

Comments for this tip:

David Mogg    12 Feb 2016, 09:18
Hi, Tried your macro, both as supplied and also with a few small changes as follows;
Sub ChangeTemplates()
    Dim strDocPath As String
    Dim strToxRep As String
    Dim strCurDoc As String
    Dim docCurDoc As Document

    ' set document folder path and template strings
    strDocPath = "C:\Users\David\Documents\Toiletries\Raw Materials\Hair Dyes\"
    strToxRep = "C:\Users\David\AppData\Roaming\Microsoft\Templates\ToxRep.dotx"
    ' get first doc - only time need to provide file spec
    strCurDoc = Dir(strDocPath & "*.docx")

    ' ready to loop (for as long as file found)
    Do While strCurDoc <> ""
        ' open file
        Set docCurDoc = Documents.Open(FileName:=strDocPath & strCurDoc)
        ' change the template
        docCurDoc.AttachedTemplate = strToxRep
        ' save and close
        docCurDoc.Close wdSaveChanges
        ' get next file name
        strCurDoc = Dir
    MsgBox "Finished"
End Sub

Doesn't work, any thoughts?

pajamauniform    31 May 2014, 19:13
Thank you!
Paradroid    08 May 2013, 10:16
Just what I needed! This'll save me a few hours of work.
awyatt    10 Dec 2012, 08:47

Many, many (MANY!) of the tips on my sites involve macros. It is unrealistic for every tip that uses macros to include instructions on how to create macros.

All is not lost, however, as there are several things you can do. First, you can use the search box at the upper-right of any page and you'll find information quickly about using macros. Second, you could go to the "Macros" category on this site and find the same information.

Finally, you could visit the WordTips FAQ, the link for which is at the right side of any page on this site. It is located here:


Look for the section entitled "How Can I Use the Macros In WordTips."

Hope that helps.

Jeff Johns    10 Dec 2012, 03:50
As always, when seeking information from anyone who is a computer programmer, I find the information to be completely useless. Why? Because there is a complete lack of information telling the reader where to paste the coding, how to paste the coding (VB editor is completely baffling), nor which lines are dummy lines which must be replaced with the specific wording (plain english or coding) to accomplish the desired end result.

Programmers fail to realize that the majority of human beings have the necessity to actually accomplish work and are not simply programmers who fiddle with coding as an obsesssive hobby.

If said coding where to save the end user two hours of work time, it would be of no use if it took the end user twenty years to find out how to use the coding, twenty years of arguing with programmers, hoping one might provide a useful answaer on rare occaision.
vijayanand    01 Nov 2012, 02:32
i need c#.net coding for attach template in word document, anyone can you please provide the sample codd

Our Company

Sharon Parq Associates, Inc.

About Tips.Net

Contact Us


Advertise with Us

Our Privacy Policy

Our Sites


Beauty and Style




DriveTips (Google Drive)

ExcelTips (Excel 97–2003)

ExcelTips (Excel 2007–2016)



Home Improvement

Money and Finances


Pests and Bugs

Pets and Animals

WindowsTips (Microsoft Windows)

WordTips (Word 97–2003)

WordTips (Word 2007–2016)

Our Products

Helpful E-books

Newsletter Archives


Excel Products

Word Products

Our Authors

Author Index

Write for Tips.Net

Copyright © 2016 Sharon Parq Associates, Inc.