简体   繁体   English

MS Word VBA:获取文档的附件模板

[英]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) (使用Windows 10和MS Word2016。全局模板为:Normal.dotx和Autoload.dotm。某些文档的附加模板为:Reference.dotx)

Hello everyone, 大家好,

I'm having problems in VBA getting the attached template of a document. 我在VBA中获取文档的附件模板时遇到问题。

I have a global template that loads when I load MS Word, called Autoload.dotm. 我有一个全局模板,该模板在加载MS Word时加载,称为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). 但是,对于某些特定文档,他们使用附加的模板,而不是全局模板(Autload.dotm)或常规模板(Normal.dotx)。 This attached template is called Reference.dotx. 该附加模板称为Reference.dotx。

So I use ActiveDocument.AttachedTemplate. 所以我使用ActiveDocument.AttachedTemplate。 But this returns Autoload.dotm, not Reference.dotx. 但这将返回Autoload.dotm,而不是Reference.dotx。 I need to find out if the attached template defined in Developer->Document Template->Templates tab->Document Template is Reference.dotx. 我需要找出在Developer-> Document Template-> Templates选项卡-> Document Template中定义的附加模板是否为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? (不要认为这会有所作为,但是选中了“自动更新文档样式”复选框。)有人知道我如何找到文档是否使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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