Word Macro To Replace Template In List Of Documents  

by Matt Hawkins, 08/10/2010
Categories : VBScript : Windows

I once needed to bulk update a whole directory of Microsoft Office Word documents to point them to a new template. They were using a template that no longer existed on a network drive and Office seemed to spend a long time looking for the non-existant template.

The following macro allows you to quickly set the template associated with a directory of Word documents to ''.

Sub ChangeTemplates()

  ' Define variables
  Dim strDocPath  As String
  Dim strTemplate As String
  Dim strCurDoc As String
  Dim docCurDoc As Document

  ' Set document folder path and template strings
  strDocPath = "d:temp"
  strTemplate = ""

  ' Get first document name
  strCurDoc = Dir(strDocPath & "*.doc")

  ' Loop through files
  Do While strCurDoc <> ""
    ' Open file
    Set docCurDoc = Documents.Open(FileName:=strDocPath & strCurDoc)
    ' Change the template
    docCurDoc.AttachedTemplate = strTemplate
    ' Save and close
    docCurDoc.Close wdSaveChanges
    ' Get next file name
    strCurDoc = Dir
  MsgBox "Finished"
End Sub

The macro assumes your documents are in directory "d:\temp\" but you can adjust this if required.

Author : Matt Hawkins  Last Edit By : Matt Hawkins
PHP Powered  MySQL Powered  Valid XHTML 1.0  Valid CSS  EUKHost - Recommended Webhosting Solutions

MattHawkins CMS v3.0 - Copyright 2009-2021