打开时如何在“Word Macro Enabled Template”中启动宏?

时间:2016-07-14 17:12:22

标签: vba ms-word macros

如何在文件打开后立即运行宏?

我想从外部数据源(Excel)“预填充”它。我知道如何填写文档,但希望宏自动运行

1 个答案:

答案 0 :(得分:3)

使用Document.Open事件。您需要将Document_Open()子文件放在文件的ThisDocument类模块中。 Example from MSDN:

Private Sub Document_Open() 
    MsgBox "This document is copyrighted." 
End Sub

或在您的情况下像

Private Sub Document_Open() 
    Call PreFillDoc
End Sub

只有在宏已经启用的情况下才会运行。