HOWTO: Use Events to Determine When Word Quits |
Q172055
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.
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
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
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. |