Microsoft KB Archive/172055
HOWTO: Use Events to Determine When Word Quits
The information in this article applies to:
- Microsoft Visual Basic Learning, Professional, and Enterprise Editions for Windows, versions 5.0, 6.0
- Microsoft Word 97 for Windows
- Microsoft Word 2000
Visual Basic 5.0 introduced a keyword, WithEvents, that enables your Visual Basic application to respond to the events triggered by an ActiveX server. This article illustrates how you can use WithEvents in your Visual Basic application to trap the Quit event of the Microsoft Word Application object.
To trap the events of an ActiveX server, you must first declare a variable that will mirror the actual object. In this example, the ActiveX object is the Word Application. The declaration appears as follows:
Dim WithEvents objWord As Word.Application
Once the Word.Application object is declared in this manner, you can respond to the events that it triggers. For example, to respond to the Quit event of objWord that references Word.Application, you can create a sub procedure named objWord_Quit.
NOTE: At the time this article is being written, Microsoft Word is the only Microsoft Office application that exposes a Quit event.
- In the Visual Basic editor, start a new "Standard EXE" project.
- Click References on the Project menu. Check "Microsoft Word 8.0 Object Library" and click OK. For Microsoft Word 2000, check "Microsoft Word 9.0 Object Library" and click OK.
- Click Add Class Module on the Project menu. The default class module name is Class1.
- Add the following code to Class1:
- Add a Label and a CommandButton to Form1.
- Add the following code to Form1:
- Press the F5 key to run the application. Click Command1. The new instance of Word is loaded. Once the application is loaded and visible, the caption of the label on Form1 reads "Word is running..." Quit Word. Once Word exits, the caption of the label changes to "Word Quit!" in response to the Quit event of Word.
Additional query words:
Keywords : kbinterop kbVBp kbVBp500 kbVBp600 kbDSupport
Issue type : kbhowto
Technology : kbWordSearch kbWordWSearch
Last Reviewed: December 5, 2000