简体   繁体   English

附加Word模板时执行宏

[英]Execute macro when Word template is attached

I want to run some code when I attach a template to a Word document. 当我将模板附加到Word文档时,我想运行一些代码。

I tried the Document_Open and Document_New methods to no avail. 我尝试了Document_OpenDocument_New方法无济于事。

I also tried the AutoNew and AutoOpen method from this article . 我也试过AutoNewAutoOpen从方法这篇文章 The AutoOpen method is triggered when I attach my template to the document, close it, and then open the document again. 当我将模板附加到文档,将其关闭,然后再次打开文档时,将触发AutoOpen方法。

How can I trigger a line of code to execute when I attach the template to a document? 将模板附加到文档时,如何触发一行代码执行?


EDIT : Currently we are attaching the template through the Templates dialog. 编辑:当前,我们通过Templates对话框附加模板。

We are also adding a button in the ribbon, with the below code, to attach the template. 我们还在功能区中添加一个带有以下代码的按钮,以附加模板。

savedTmplt = "C:\Program Files\*****\template.dotm"

Set q = wd.ActiveDocument    
wd.WordBasic.DisableAutoMacros False

With q
    .AttachedTemplate = savedTmplt
    .UpdateStyles
End With

wd.WordBasic.DisableAutoMacros

Whilst there is an Open event and a New event there is'nt an AttachTemplate event. 虽然有一个Open事件和一个New事件,但没有AttachTemplate事件。 So it simply isn't possible to have code automatically respond to the template being attached. 因此,根本不可能让代码自动响应所附加的模板。

Your options are: 您的选择是:

  • Attach the template then close and reopen the document. 附加模板,然后关闭并重新打开文档。
  • Attach the template then explicitly call the routine you want to run. 附加模板,然后显式调用要运行的例程。

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

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