【问题标题】:MS Word VBA: Get document's attached templateMS Word VBA:获取文档的附加模板
【发布时间】:2017-03-19 21:41:13
【问题描述】:

(使用 Windows 10 和 MS Word 2016。全局模板为:Normal.dotx 和 Autoload.dotm。某些文档的附加模板为:Reference.dotx)

大家好,

我在 VBA 获取文档附件模板时遇到问题。

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

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

我用来尝试获取附加模板的代码很简单:

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

【问题讨论】:

  • 会不会是路径不对?您可以使用AttachedTemplate.Name and .Path。你试过Debug.print activedocument.attachedtemplate.name.path吗?
  • 不。不幸的是,这只是返回 Normal.dotm。它甚至不返回我添加的全局模板。:/
  • 这正是问题所在(?)。它找不到文档reference.dotx。
  • 天哪,原来如此!我最近换了电脑,所以路径弄乱了,我检查模板时唯一注意的是文件名。我在路上装上了眼罩。非常感谢您解决这个问题!

标签: vba templates ms-word


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-12
    • 2014-07-24
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 2020-03-28
    • 1970-01-01
    相关资源
    最近更新 更多