VBA Code


Documents can sometimes lose their connection to their template
Need to re-attach if invalid or cannot be found.


Application.organizercopy 

There is a templates collection for all the templates

Dim objTemplate As Template 
For Each objTemplate In Application.Templates
Next objTemplate


You cannot create a new template using the templates collection.
Templates are created by first creating a document and then saving it as a template, using SaveAs

ActiveDocument.SaveAs "newtemplate" wdFormatTemplate 


A template is attached to the templates collection whenever you:

  • Use the Documents.Open method

  • Use the Documents.Add method

  • Use the AttachedTemplate property to change the template for a document (i.e. replacing the existing template)


You can use the Application.NormalTemplate property to return a template object that refers to the Normal Template

Dim objTemplate As Template 
Set objTemplate = Application.NormalTemplate


Returns the template attached to the active document

Dim objTemplate As Template 
Set objTemplate = ActiveDocument.AttachedTemplate


Creates a new document from an existing document

Documents.Add Template:="C:\Temp\Template.doc" 
              NewTemplate:=True / False


ActiveDocument.AttachedTemplate.Name 
ActiveDocument.AttachedTemplate.FullName

Opens a template that is in the Templates collection as a document.

ActiveDocument.AttachedTemplate.OpenAsDocument 

Dim objDocument As Document 
   Set objDocument = ActiveDocument.AttachedTemplate.OpenAsDocument
   If objDocument.Content.Text = vbCr Then
'document does not contain any text
   Else
'document does contain text
   End If
   objDocument.Close SaveChanges:=wdSaveOptions.wdDoNotSaveChanges

The following code saves a copy of the Normal.dotm template

Set objDocument = NormalTemplate.OpenAsDocument 
objDocument.SaveAs FileName:="C:\Temp\Backup.dot"
objDocument.Close SaveChanges:=wdSaveOptions.wdDoNotSaveChanges


Copying styles from a template

objDocument.CopyStylesFromTemplate 


Using the Organiser

Application.OrganizerCopy Source:="C:\Temp\Normal.dotm", _ 
                          Destination:="C:\Temp\Document.doc", _
                          Name:="StyleName_1", _
                          Object:=wdOrganizerObject.wdOrganizerObjectStyles


© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext