(使用Windows 10和MS Word2016。全局模板为:Normal.dotx和Autoload.dotm。某些文档的附加模板为:Reference.dotx)
大家好,
我在VBA中获取文档的附件模板时遇到问题。
我有一个全局模板,该模板在加载MS Word时加载,称为Autoload.dotm。但是,对于某些特定文档,它们使用附加的模板,而不是全局模板(Autload.dotm)或常规模板(Normal.dotx)。该附加模板称为Reference.dotx。
所以我使用ActiveDocument.AttachedTemplate。但这将返回Autoload.dotm,而不是Reference.dotx。我需要找出在“开发人员”->“文档模板”->“模板”选项卡->“文档模板”中定义的附加模板是否为Reference.dotx。(不要认为这会有所作为,但是选中了“自动更新文档样式”复选框。)有人知道我如何找到文档是否使用Reference.dotx吗?我不需要任何返回的全局模板。
我用来获取附件模板的代码很简单:
If (ActiveDocument.AttachedTemplate = "Reference.dotx") Then
PrepareDocument_enabled = True
End If
也许这对您有帮助?它将显示使用的模板。
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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句