2017-03-26 44 views
0

我想創建App_DocumentBeforeClose。App_DocumentBeforeClose vba word

在的ThisDocument:

Private Sub Document_Open() 
start 
End Sub 

在MODUL

Sub start() 
Dim X As New EventClassModule 
Set X.App = Word.Application 
End Sub 

在課堂Mudel:

我用這個代碼中使用

Public WithEvents App As Word.Application 

Private Sub App_DocumentBeforeClose(ByVal Doc As Document, Cancel As 
Boolean) 
MsgBox "App_DocumentBeforeClose" 
End Sub 

當我關閉文檔這不是讓我看看MsgBox ,爲什麼?

感謝,

塔爾

+0

你必須實例化類:https://msdn.microsoft.com/en-us/library/office/ff821218.aspx#Initialize – Absinthe

回答

0

你的子正常運行,但X被實例化爲過程範圍的變量,因此一旦start()結束,X超出範圍,而不是提供給任何後續DocumentBeforeClose()事件觸發

你必須申報X作爲Public變量

Public X As New EventClassModule '<--| this way 'X' is "seen" throughout the entire life of the document 


Sub start() 
' Dim X As New EventClassModule '<--| this way 'X' would be "seen" only at the moment 'start()' is running 
    Set X.App = Word.Application 
End Sub 
+0

@tal_sshh,你通過它嗎? – user3598756