Microsoft KB Archive/172055

From BetaArchive Wiki
< Microsoft KB Archive
Revision as of 10:05, 21 July 2020 by X010 (talk | contribs) (Text replacement - """ to """)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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.

Step-by-Step Example

NOTE: At the time this article is being written, Microsoft Word is the only Microsoft Office application that exposes a Quit event.

  1. In the Visual Basic editor, start a new "Standard EXE" project.
  2. 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.
  3. Click Add Class Module on the Project menu. The default class module name is Class1.
  4. Add the following code to Class1:
  5. Add a Label and a CommandButton to Form1.
  6. Add the following code to Form1:
  7. 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
© 2001 Microsoft Corporation. All rights reserved. Terms of Use.