vb6word-automation

How do I stop the user from manually closing a VB6 automated instance of Word?


My question is about absolute control over automated instances. I'm using VB6 to automate the generation of forms used in our workplace. The app is in Beta and I've written a User Manual to introduce the application to new users; explaining the sub-functions of the GUI. I use a command button on the GUI to open and close the User Manual in an instance of Word. All this is fine until the user closes the Word app manually while the app is running. This kills the Word instance, but I need to either stop the user from closing the Word instance, or have the app realize the instance is gone. My automation knowledge is pretty shallow. I adapt sub routines from VBA macros. Please help.


Solution

  • I need to either stop the user from closing the Word instance, or have the app realize the instance is gone

    You can do both - declare your Word variable using the "Give me events" syntax and it will raise the DocumentBeforeClose event in your code.

    Public WithEvents mWordApp As Word.Application
    
    Sub DoStuff()
        Set mWordApp = New Word.Application
        '// open doc ...
        mWordApp.Visible = True
    End Sub
    
    Private Sub mWordApp_DocumentBeforeClose(ByVal Doc As Document, Cancel As Boolean)
        Cancel = MsgBox("Word is closing, keep open?", vbYesNo) = vbYes
    End Sub