Hack 48 Attach the Same Template to Multiple FilesUse a macro to update or change the template for all the files in a folder. When documents are passed around among reviewers or contributors, you'll often want to reattach the correct template when you get the files back. Doing that for more than a few files can be a real chore, though. This hack shows how to attach the template used by the current document to all the documents in the same folder as the current document. 5.5.1 The Code
Place this macro in the template of your choice [Hack #50]
and either run it from the Tools Sub DocTemplateToAllFilesInFolder( ) Dim i As Integer Dim doc As Document Dim sFolder As String Dim oTemplate As Template Dim sFileFullName As String Dim sFileName As String sFolder = ActiveDocument.Path If Len(sFolder) = 0 Then MsgBox "Please save this document first" Exit Sub End If Set oTemplate = ActiveDocument.AttachedTemplate With Application.FileSearch .NewSearch .LookIn = sFolder .SearchSubFolders = False .FileType = msoFileTypeWordDocuments If Not .Execute( ) = 0 Then For i = 1 To .FoundFiles.Count sFileFullName = .FoundFiles(i) sFileName = Right(sFileFullName, _ (Len(sFileFullName) - _ (InStrRev(sFileFullName, "\")))) If sFileName Like "[!~]*" Then If Not sFileName = ActiveDocument.Name Then Set doc = Documents.Open(sFileFullName) doc.AttachedTemplate = oTemplate doc.UpdateStyles doc.Save doc.Close Set doc = Nothing End If End If Next i Else MsgBox "No files found" End If End With End Sub This macro is just a modified version of the code demonstrated in [Hack #59] . 5.5.2 Hacking the Hack
Another common scenario is needing to attach the same template to several open files, which may not be in the same folder. The following macro attaches the template used by the current document to all the other open documents. Place this macro in the template of your choice [Hack #50]
and either run it from the Tools Sub ThisTemplateToAllOpenDocs( ) Dim i As Integer Dim oTemplate As Template Set oTemplate = ActiveDocument.AttachedTemplate For i = 1 To Documents.Count If Not Documents(i).FullName = ActiveDocument.FullName Then Documents(i).AttachedTemplate = oTemplate Documents(i).UpdateStyles End If Next i End Sub |