HOWTO: Use Events to Determine When Word Quits

ID: Q172055


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


SUMMARY

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.


MORE INFORMATION

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. 
          Option Explicit
    
          Dim WithEvents objWord As Word.Application
    
          Private Sub Class_Initialize()
    
             Form1.Label1.Caption = "Starting Word..."
    
             'Instantiate a new instance of Word and add
             'a new document
             Set objWord = New Word.Application
             objWord.Documents.Add
             objWord.Visible = True
    
             Form1.Label1.Caption = "Word is running..."
    
          End Sub
    
          Private Sub objWord_Quit()
    
             'Respond to the Quit event of Word
             Form1.Label1.Caption = "Word Quit!"
    
          End Sub 
  6. Add a Label and a CommandButton to Form1.


  7. Add the following code to Form1:


  8. 
         Option Explicit
    
          Dim objAppWithEvents As Class1
    
          Private Sub Command1_Click()
             'Instantiate Class1
             Set objAppWithEvents = New Class1
          End Sub
    
          Private Sub Form_Unload(Cancel As Integer)
             Set objAppWithEvents = Nothing
          End Sub
     
  9. 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
Version : WINDOWS:2000,5.0,6.0,97
Platform : WINDOWS
Issue type : kbhowto


Last Reviewed: November 18, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.