Share via

Word VBA: get location and or name of attached template when the original template it was based on, is no longer available

Anonymous
2023-01-24T14:32:35+00:00

Context:

  • Document created based on a template in a subfolder of the Workgrouptemplates location
  • Workgrouptemplates folder is in user location
  • Connection to template gets lost when another user opens the document

The situation above works as advertised. The connection to the template can't be found and therefor ribbon customizations etc. are not available anymore.

I am explicitly not looking for an alternative way of installing workgrouptemplates.

The soon as you open this document, the attachedtemplate reverts to Normal.dotm. I would like to know if there is a way to find the path (or at least name) of the original template that was attached.

It is in the documentproperties if you look at document in Windows Explorer, but the moment you open the file this info seems lost from within Word itself.

Is there any way in VBA to get the original template location/name?

Microsoft 365 and Office | Word | For business | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

5 answers

Sort by: Most helpful
  1. Anonymous
    2023-01-24T16:36:38+00:00

    Hi Jay,

    Thank you!

    This was exactly the alternative route I thought about.

    I find it only very hard to understand why it is that the info is in the document somewhere, and that I can't think of a way to get it with VBA ;-)

    Astrid

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  2. Jay Freedman 207.7K Reputation points Volunteer Moderator
    2023-01-24T16:23:36+00:00

    Another way to approach this is to add this macro to the template's ThisDocument module:

    Private Sub Document_New() 
    
        ActiveDocument.CustomDocumentProperties.Add _ 
    
            Name:="OriginalTemplate", LinkToContent:=False, _ 
    
            Value:=ThisDocument.FullName, Type:=msoPropertyTypeString 
    
    End Sub 
    

    This macro will run automatically each time a new document is created based on this template (and at no other time). That means that the property's value won't change when another template (which doesn't have code to overwrite this particular property) is attached.

    There are a couple of ways to retrieve the custom property's value, regardless of whether the document has been attached to a different template.

    • Insert the field { DOCPROPERTY OriginalTemplate } anywhere in the document and update it.
    • Go to File > Info, click the Properties heading at the top right, and click Advanced Properties in the menu. In the Properties dialog that opens, go to the Custom tab and select OriginalTemplate in the Properties list. The full name of the original template will appear in the Value box (which can scroll sideways).

    Image

    • In VBA, if you want the value for use in another macro, retrieve the value into a string variable with code like this:
        Dim OT As String 
      
        OT = ActiveDocument.CustomDocumentProperties("OriginalTemplate").Value
      

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  3. Anonymous
    2023-01-24T16:19:23+00:00

    I see that the name of the template is stored in the docProps\app.xml part of the document

     <Properties xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes" xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties">
    
    <Template>Test.dotm</Template>
    

    You would think the path itself must be stored somewhere as well, since it is there in the document. If you set the document as read/only before you open the file in Word itself, the full path and filename of the attachedtemplate are visible in the interface.

    Is it possible to retrieve the template property from xml when the document itself is opened?

    Astrid

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2023-01-24T16:03:14+00:00

    Hi John,

    Thanks for your reply.

    This sounded very promising. However it does not work.

    As soon as I open the document, the template attaches to normal.

    I tried the AttachedTemplate earlier, but as a property of the document itself:

    ActiveDocument.AttachedTemplate.fullname
    

    I thought defining an object variable to a template might be different. In both cases however, the result is normal.dotm instead of the original template.

    Now that I think about it more, I realise this couldn't work at all. Since the path/filename is not available, you can't create a template object from it as well.

    Astrid

    Was this answer helpful?

    0 comments No comments
  5. John Korchok 232.4K Reputation points Volunteer Moderator
    2023-01-24T15:46:15+00:00

    This code will work on a document that hasn't yet been re-attached to the Normal template:

    Set myTemplate = ActiveDocument.AttachedTemplate 
    
    MsgBox myTemplate.Path & Application.PathSeparator & myTemplate.Name
    

    Reference: Document.AttachedTemplate property (Word)

    Was this answer helpful?

    0 comments No comments