0

In MS Word we have a template document (template1.docm). This document is use in SharePoint by many users to write thousands of procedure documents, saved as .mht documents.

As we update styles in template we would like to update all these other documents created from it.

Is it possible to update styles in just this one template file and have all these other files automatically update?

If not, what else can I do to update these thousands of documents with the changes to the template file?

music2myear
  • 40,472
  • 44
  • 86
  • 127

1 Answers1

0

I will try to outline an approach to the problem using a VBA macro, warning that it is entirely theoretical and untested.

The idea is to create a work document containing a VBA macro that does the work of re-attaching the template to all the documents in a folder, after asking for the folder.

Do not put the file containing this macro in the folder you want to process, or it will process itself.

Here is the macro :

Sub UpdateDocuments()
Dim strFolder As String, strFile As String, strCurDoc As Document, strTemplate As String
strFolder = GetFolder
strTemplate = "C:\path to template\template.dot"
If strFolder = "" Then Exit Sub
Application.ScreenUpdating = False
strFile = Dir(strFolder & "\*.docx", vbNormal)
While strFile <> ""
  Set strCurDoc = Documents.Open(FileName:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)
  strCurDoc.AttachedTemplate = strTemplate
  strCurDoc.Close wdSaveChanges
  End With
  strFile = Dir()
Wend
Set wdDoc = Nothing
Application.ScreenUpdating = True
End Sub

Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function
harrymc
  • 455,459
  • 31
  • 526
  • 924
  • Not as good as I thought but... Problem solves with a single line : `ActiveDocument.CopyStylesFromTemplate("C:\Temp\FullPathToTemplate.dotx")`. I use a template with new styles and copy them with a macro in other document. – Patix80 Oct 20 '18 at 19:41
  • Do you use a solution similar to the above, or do you intend to add a new macro into thousands of documents? – harrymc Oct 20 '18 at 19:46
  • In fact, a macro code file is include in many others. So, it's easy to add this code in code file only. We must however, edit ribbon to add the new macro button... I was not able to use your solution for now, but I keep it for the next step! thanks – Patix80 Oct 20 '18 at 19:55