2016-11-06 89 views
0

(使用Windows 10和MS Word 2016年全球模板是:Normal.dotx和Autoload.dotm附加的模板有些文档是:Reference.dotx)微软Word VBA:获取文档的附加模板

大家好,

我在VBA中获取文档的附加模板时出现问题。

我有一个全局模板,当我加载MS Word时调用Autoload.dotm。但是,对于某些特定的文档,它们使用附加的模板,而不是全局模板(Autload.dotm)或常规模板(Normal.dotx)。这个附加模板被称为Reference.dotx。

所以我使用ActiveDocument.AttachedTemplate。但是这会返回Autoload.dotm,而不是Reference.dotx。我需要找出在Developer-> Document Template-> Templates选项卡 - >文档模板中定义的附加模板是否为Reference.dotx。 (不要以为它会有所作为,但选中“自动更新文档样式”复选框。)有谁知道如何查找文档是否使用了Reference.dotx?我不需要任何返回的全局模板。

我使用的是设法得到附加的模板的代码很简单:

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

难道说是错误的路径?你可以使用'AttachedTemplate.Name和.Path'。你有没有尝试过'Debug.print activedocument.attachedtemplate.name'或'.path'? – Niclas

+0

Nah。不幸的是,只是返回Normal.dotm。它甚至不会返回我添加的全局模板。:/ –

+1

而这正是问题(?)。它无法找到文档reference.dotx。 – Niclas

回答

0

也许这将帮助你?它会显示使用的模板。

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

否则,你可以用它来改变模板

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