2017-03-26 41 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