我的 excel 檔案中有一個代碼,它打開某個 Word 檔案以從中讀取資料,并且該 Word 檔案還有一個“Document_Open”事件處理程式(每次打開該 Word 檔案時都會顯示一個用戶表單)。
我的問題是,由于“Document_Open”事件處理程式,每次我運行 Excel 代碼打開該 Word 檔案時,都會控制卡在 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
uj5u.com熱心網友回復:
根據https://wordmvp.com/FAQs/InterDev/DisableAutoMacros.htm 中的文章,您可以使用以下命令禁用/啟用自動宏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
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/345280.html
