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.
by Allen Wyatt
(last updated November 26, 2016)
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 Loop 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 " & _ "http://support.microsoft.com/kb/224351" GoTo FinishUp End If 'Get the template names strFindTemplate = UCase(InputBox("Name of template to find (exclude the .dot)") & _ ".dot") strReplaceTemplate = InputBox("Name of replacement template (exclude the .dot)") & _ ".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 Wend 'Report the results If strAffectedDocs = "" Then MsgBox "No documents were changed.", , "Template Batch Change" Else '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 ErrorHandler: Set objThisDoc = Nothing Set objPropertyReader = Nothing Err.Raise vbError + 1001, "TemplateBatchChange", _ "TemplateBatchChange encountered an error: " & Err.Description FinishUp: '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.
Do More in Less Time! Are you ready to harness the full power of Word 2013 to create professional documents? In this comprehensive guide you'll learn the skills and techniques for efficiently building the documents you need for your professional and your personal life. Check out Word 2013 In Depth today!
Templates are used to store styles and lots of other customizations that affect how you use Word. On a single-user machine, ...Discover More
If your Normal.dot file is continually saved when you exit Word, even when you haven't made any changes to it, the culprit ...Discover More
When you make changes that affect a template, Word usually asks you if you want to save those changes when you exit the ...Discover More
Got a version of Word that uses the menu interface (Word 97, Word 2000, Word 2002, or Word 2003)? This site is for you! If you use a later version of Word, visit our WordTips site focusing on the ribbon interface.