How To Handle Events for Microsoft Word Using Microsoft Visual Basic .NET

This article was previously published under Q302816
This article has been archived. It is offered "as is" and will no longer be updated.
For a Microsoft Visual C# .NET version of this article, see 302817.
This article demonstrates how to handle Word events from an Automation client that is created with Visual Basic .NET.

Create an Automation Client that Handles Word Events

  1. Start Visual Studio .NET.
  2. On the File menu, point to New and then click Project.
  3. Select Windows Application from the Visual Basic Projects types, accept the default project name and location, and then click OK. Form1 is created by default.
  4. Create a reference to the Microsoft Word Object Library. To do this, follow these steps:
    1. On the Project menu, click Add Reference.
    2. On the COM tab, locate Microsoft Word Object Library and click Select.NOTE: If you have not already done so, Microsoft recommends that you download and install the Microsoft Office XP Primary Interop Assemblies (PIAs). For additional information about Office XP PIAs, click the article number below to view the article in the Microsoft Knowledge Base:
      328912 INFO: Microsoft Office XP PIAs Are Available for Download
    3. Click OK in the Add References dialog box to accept your selections. If you are prompted to generate wrappers for the selected libraries, click Yes.
  5. On the View menu, click Toolbox and add a button to Form1.
  6. Double-click Button1. The code window opens at the Button1_Click event.
  7. Add the following to the top of Form1.vb:
    Imports Microsoft.Office.Interop					
  8. In the code window, paste the following code above the Button1_Click event:
    Private WithEvents oWord As Word.ApplicationClass					
  9. Add the following code to the Button1_Click event:
       'Create a new instance of Word, make it visible, and activate it.   oWord = CreateObject("Word.Application")   oWord.Visible = True   oWord.Activate()   'Create a new document.   oWord.Documents.Add()   'Release the instance of Word and leave it running.   oWord = Nothing					
  10. Add the following code comments after the Button1_Click event:
       'Handler for the Microsoft Word NewDocument event.   'The NewDocument event is fired when a new document is created.					
  11. Create the event code. To do this, select oWord in the Class Name list, and then select ApplicationEvents3_Event_NewDocument in the Method Name list. The following event code is created:
    Private Sub oWord_ApplicationEvents3_Event_NewDocument(ByVal Doc As Word._Document) _   Handles oWord.ApplicationEvents3_Event_NewDocument   End Sub 					
  12. Enter the following in the NewDocument event:
       'Add some text to the new document.   With oWord.Selection      .TypeText("The ")      With .Font         .Bold = Word.WdConstants.wdToggle         .Italic = Word.WdConstants.wdToggle      End With      .TypeText("NewDocument ")      With .Font         .Bold = Word.WdConstants.wdToggle         .Italic = Word.WdConstants.wdToggle      End With      .TypeText("event handler inserted this text.")   End With					
  13. Press F5 to run the application.
  14. Click Button1. Note that Word opens with Document1 displaying "The NewDocument event handler inserted this text."
Visual Basic .NET uses a standard naming convention for event handlers that combines the name of the event sender, an underscore, and the name of the event. The Handles keyword is used to declare that a procedure handles a specified event. In the above example, oWord is the object that generates and sends events and NewDocument is the name of the event that is being handled.

Events Generated in Word 2000

CloseDocumentOccurs when a document is closed.
DocumentBeforeCloseApplication Occurs immediately before any open document closes.
DocumentBeforePrintApplicationOccurs immediately before any open document is printed.
DocumentBeforeSaveApplicationOccurs before any open document is saved.
DocumentChangeApplicationOccurs when a new document is created, when an existing document is opened, or when another document is made the active document.
DocumentOpenApplicationOccurs when a document is opened.
NewDocumentOccurs when a new document based on the template is created. A procedure for the New event runs only if it is stored in a template.
NewDocumentApplicationOccurs when a new document is created.
OpenDocumentOccurs when a document is opened.
QuitApplicationOccurs when the user quits Word.
WindowActivateApplicationOccurs when any document window is activated.
WindowBeforeDoubleClickApplicationOccurs when the editing area of a document window is double-clicked, before the default double-click action.
WindowBeforeRightClickApplicationOccurs when the editing area of a document window is right-clicked, before the default right-click action.
WindowDeactivateApplicationOccurs when any document window is deactivated.
WindowSelectionChangeApplicationOccurs when the selection changes in the active document window.

Additional Events Generated in Word 2002

EPostageInsertApplicationOccurs when a user inserts electronic postage into a document.
EPostagPropertyDialogApplicationOccurs when a user clicks E-postage Properties (on the Labels and Envelopes dialog box) or Print Electronic Postage. This event allows a third-party software application to intercept and show its Properties dialog box.
MailMergeAfterMergeApplicationOccurs after all records in a mail merge have merged successfully.
MailMergeRecordMergeApplicationOccurs after each record in the data source successfully merges in a mail merge.
MailMergeBeforeMergeApplicationOccurs when a merge is executed before any records merge.
MailMergeBeforeRecordMergeApplicationOccurs as a merge is executed for the individual records in a merge.
MailMergeDataSourceLoadApplicationOccurs when the data source is loaded for a mail merge.
MailMergeDataSourceValidateApplicationOccurs when a user performs address verification by clicking Validate in the Mail Merge Recipients dialog box.
MailMergeWizardSendToCustomApplicationOccurs when the custom button is clicked on step six of the Mail Merge Wizard.
MailMergeWizardStateChangeApplicationOccurs when a user changes from a specified step to a specified step in the Mail Merge Wizard.
WindowSizeApplicationOccurs when the application window is resized or moved.
For more information, see the following Microsoft Developer Network (MSDN) Web site:
Microsoft Office Development with Visual Studio
For additional information about handling events in Word with Automation, click the article number below to view the article in the Microsoft Knowledge Base:
285333 INFO: Word 2002 MailMerge Event Code Demonstration

Article ID: 302816 - Last Review: 12/06/2015 03:40:56 - Revision: 3.2

Microsoft Visual Basic .NET 2002 Standard Edition, Microsoft Word 2002

  • kbnosurvey kbarchive kbautomation kbhowto KB302816