DocumentBeforeClose syntax

前端 未结 2 2002
情歌与酒
情歌与酒 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!

    0 讨论(0)
  • 2021-01-24 09:44

    X is an instance of the class you created (EventClassModule)

    Your problem is that .App is not a property of EventClassModule. Change

    Set X.App = Word.Application 
    

    to the property you defined in your class

    Set X.appWord = Word.Application 
    
    0 讨论(0)
提交回复
热议问题