DocumentBeforeClose syntax

前端 未结 2 2001
情歌与酒
情歌与酒 2021-01-24 09:16

I really don\'t know the syntax of the DocumentBeforeClose event. Following this page, I should create a class module called \'EventClassModule\' (see also this article). So I d

2条回答
  •  星月不相逢
    2021-01-24 09:40

    I tried the same thing! Actually it works for me. I have this in a class module named EventClassModule

    Public WithEvents appWord As Word.Application
    
    Private Sub appWord_DocumentBeforeClose _
            (ByVal Doc As Document, _
            Cancel As Boolean)
    
    'Here is the code you want to do before it close
    MsgBox "WORKING!"
    
    End Sub
    

    And in a module (not a class module) I have this

    Dim X As New EventClassModule
    
    Sub AutoExec()
    
        'Call any other sub or function you want
    
        Call Register_Event_Handler
    
    End Sub
    
    Sub Register_Event_Handler()
    
        Set X.appWord = Word.Application
    
    End Sub
    

    AutoExec is called as soon as the document is loaded. So it calls the sub Register_Event_Handler to register the object X (which is a object EventClassModule, the class module created). So X will be annonced that the document is about to close

    hope it helps!

提交回复
热议问题