首页 > 解决方案 > 在 AutoNew 子中附加模板的问题

问题描述

我在 VBA MS Word 中编写了一个宏来自动在新文档中附加模板:

Sub AutoNew()
    ActiveDocument.AttachedTemplate = <Path To Template>
    ActiveDocument.UpdateStylesOnOpen = True
End Sub

但是,此宏无法正常工作。它附加模板,模板工作中的宏,但它不更新样式。只有 Normal 模板中的样式可用。你能告诉我我能做什么吗?提前致谢

标签: vbatemplatesms-word

解决方案


颠倒命令的顺序。您需要告诉文档从模板更新样式,然后附加它。

我会推荐:

Sub AutoNew()
    Let ActiveDocument.UpdateStylesOnOpen = True
    Let ActiveDocument.AttachedTemplate = <Path To Template>
    Let ActiveDocument.UpdateStylesOnOpen = False ' Once styles are imported, turn off this attribute
End Sub
  • 请注意关闭该设置的结尾。这是保持活动状态的糟糕设置,它与文档保持一致。

像 Timothy Rylatt 一样,我想知道您为什么不简单地根据相关模板开始文档。然后样式已经存在。


推荐阅读