HOWTO: Use Events to Determine When Word Quits

Last reviewed: September 29, 1997
Article ID: Q172055
The information in this article applies to:
  • Microsoft Visual Basic Learning, Professional, and Enterprise Editions for Windows, version 5.0
  • Microsoft Word 97 for Windows

SUMMARY

Visual Basic 5.0 introduces 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: Microsoft Word 97 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.

  3. Click Add Class Module on the Project menu. The default class module name is Class1.

  4. Add the following code to Class1:

          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
    
    

  5. Add a Label and a CommandButton to Form1.

  6. Add the following code to Form1:

          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
    
    

  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.
Keywords          : vb5all vb5howto vbwin GnrlVb kbprg
Technology        : kbvba
Version           : WINDOWS:5.0 97
Platform          : WINDOWS
Issue type        : kbhowto


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: September 29, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.