Doc937
Doc937

Reputation: 21

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.

Upvotes: 2

Views: 78

Answers (2)

Doc937
Doc937

Reputation: 21

The "... WithEvents ..." declaration of the MSOffice Application instance is the solution to this problem. The event list for the app allows the programmer full automated control of the instance from behind the curtain. Thanks all!

Upvotes: 0

Alex K.
Alex K.

Reputation: 175776

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

Upvotes: 5

Related Questions