打开 Word 文档时防止 MS-Word 应用程序使用 运行 自动宏

Prevent MS-Word application from running auto macro when opening a Word document

我的 excel 文件中有一个代码可以打开某个 Word 文档以从中读取数据,并且该 Word 文档还有一个“Document_Open”事件处理程序(它显示一个用户窗体当我打开那个 Word 文档时)。

我的问题是,每次我 运行 我的 Excel 代码打开那个 Word 文件时,由于那个“Document_Open”事件处理程序,代码的控制卡在 Word 中环境。

到目前为止我的代码:

Private Sub CommandButton18_Click()

Dim Word As New Word.Application
Dim MRpt As Word.Document

Word.Visible = True
Set MRpt = Documents.Open(Filename:="MyWordDocumentPath" &".docm")

'At This Point code will be stuck because my UserForm inside my Word 
'document would show up and it prevents the code in Excel to continue)

End Sub

根据 https://wordmvp.com/FAQs/InterDev/DisableAutoMacros.htm 中的文章,您可以 disable/enable 使用 WordBasic.DisableAutoMacros 自动宏:

Private Sub CommandButton18_Click()
   Dim Word As New Word.Application
   Dim MRpt As Word.Document

   Word.Visible = True
   Word.WordBasic.DisableAutoMacros 1 'Disable
   Set MRpt = Documents.Open(Filename:="MyWordDocumentPath" &".docm")

   'At This Point code will be stuck because my UserForm inside my Word 
   'document would show up and it prevents the code in Excel to continue)

   Word.WordBasic.DisableAutoMacros 0 'Enable back
End Sub