简体   繁体   中英

MS Word VBA: Get document's attached template

(Using Windows 10 and MS Word 2016. Global templates are: Normal.dotx and Autoload.dotm. Attached template to some docs is: Reference.dotx)

Hello everyone,

I'm having problems in VBA getting the attached template of a document.

I have a global template that loads when I load MS Word, called Autoload.dotm. But, for some specific documents, they use an attached template, which is not the global template (Autload.dotm) or the regular template (Normal.dotx). This attached template is called Reference.dotx.

So I use ActiveDocument.AttachedTemplate. But this returns Autoload.dotm, not Reference.dotx. I need to find out if the attached template defined in Developer->Document Template->Templates tab->Document Template is Reference.dotx. (Don't think it makes a difference, but the "Automatically update document styles" checkbox is checked.) Does anyone know how I can find if a document uses Reference.dotx? I don't need any of the global templates returned.

The code I'm using to try to get the attached template is simple:

    If (ActiveDocument.AttachedTemplate = "Reference.dotx") Then
        PrepareDocument_enabled = True
    End If

Maybe this will help you? It will show the template used.

Sub Macro1()
Dim strPath As String
    strPath = Dialogs(wdDialogToolsTemplates).Template
    MsgBox strPath
End Sub

Otherwise, you can use this to change the template

Sub ChangeAttachedTemplate()
Dim oDoc As Document
Dim oTemplate As Template
Dim strTemplatePath As String

Set oDoc = ActiveDocument

If oDoc.Type = wdTypeTemplate Then Exit Sub

Set oTemplate = oDoc.AttachedTemplate

Debug.Print oTemplate.FullName

' Path is probably: C:\Users\USERNAME\AppData\Roaming\Microsoft\Templates\
If InStr(UCase(oTemplate.FullName), UCase("Path of the template")) > 0 Then
     oDoc.AttachedTemplate = "PATH TO TEMPLATE" & "TEMPLATE NAME.dotm"
End If
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM