Microsoft KB Archive/302816

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

Article ID: 302816

Article Last Modified on 3/29/2007

-

APPLIES TO


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

-



This article was previously published under Q302816



For a Microsoft Visual C# .NET version of this article, see 302817.



SUMMARY
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
 Start Visual Studio .NET. On the File menu, point to New and then click Project. 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. Create a reference to the Microsoft Word Object Library. To do this, follow these steps:  On the Project menu, click Add Reference. 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

</li> 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.</li></ol> </li> On the View menu, click Toolbox and add a button to Form1.</li> Double-click Button1. The code window opens at the Button1_Click event.</li>  Add the following to the top of Form1.vb: Imports Microsoft.Office.Interop </li>  In the code window, paste the following code above the Button1_Click event: Private WithEvents oWord As Word.ApplicationClass </li>  Add the following code to the Button1_Click event: 'Create a new instance of Word, make it visible, and activate it. oWord = CreateObject(&quot;Word.Application&quot;) oWord.Visible = True oWord.Activate

'Create a new document. oWord.Documents.Add

'Release the instance of Word and leave it running. oWord = Nothing </li>  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. </li>  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 </li>  Enter the following in the NewDocument event: 'Add some text to the new document. With oWord.Selection .TypeText(&quot;The &quot;) With .Font .Bold = Word.WdConstants.wdToggle .Italic = Word.WdConstants.wdToggle End With .TypeText(&quot;NewDocument &quot;) With .Font .Bold = Word.WdConstants.wdToggle .Italic = Word.WdConstants.wdToggle End With .TypeText(&quot;event handler inserted this text.&quot;) End With </li> Press F5 to run the application.</li> Click Button1. Note that Word opens with Document1 displaying &quot;The NewDocument event handler inserted this text.&quot;</li></ol>

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.

Additional Events Generated in Word 2002
<div class="references_section">