Microsoft KB Archive/302816

From BetaArchive Wiki

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.

MORE INFORMATION

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

Event Object Description
Close Document Occurs when a document is closed.
DocumentBeforeClose Application Occurs immediately before any open document closes.
DocumentBeforePrint Application Occurs immediately before any open document is printed.
DocumentBeforeSave Application Occurs before any open document is saved.
DocumentChange Application Occurs when a new document is created, when an existing document is opened, or when another document is made the active document.
DocumentOpen Application Occurs when a document is opened.
New Document Occurs 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.
NewDocument Application Occurs when a new document is created.
Open Document Occurs when a document is opened.
Quit Application Occurs when the user quits Word.
WindowActivate Application Occurs when any document window is activated.
WindowBeforeDoubleClick Application Occurs when the editing area of a document window is double-clicked, before the default double-click action.
WindowBeforeRightClick Application Occurs when the editing area of a document window is right-clicked, before the default right-click action.
WindowDeactivate Application Occurs when any document window is deactivated.
WindowSelectionChange Application Occurs when the selection changes in the active document window.

Additional Events Generated in Word 2002

Event Object Description
EPostageInsert Application Occurs when a user inserts electronic postage into a document.
EPostagPropertyDialog Application Occurs 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.
MailMergeAfterMerge Application Occurs after all records in a mail merge have merged successfully.
MailMergeRecordMerge Application Occurs after each record in the data source successfully merges in a mail merge.
MailMergeBeforeMerge Application Occurs when a merge is executed before any records merge.
MailMergeBeforeRecordMerge Application Occurs as a merge is executed for the individual records in a merge.
MailMergeDataSourceLoad Application Occurs when the data source is loaded for a mail merge.
MailMergeDataSourceValidate Application Occurs when a user performs address verification by clicking Validate in the Mail Merge Recipients dialog box.
MailMergeWizardSendToCustom Application Occurs when the custom button is clicked on step six of the Mail Merge Wizard.
MailMergeWizardStateChange Application Occurs when a user changes from a specified step to a specified step in the Mail Merge Wizard.
WindowSize Application Occurs when the application window is resized or moved.


REFERENCES

For more information, see the following Microsoft Developer Network (MSDN) Web site:

Microsoft Office Development with Visual Studio
http://msdn2.microsoft.com/en-us/library/aa188489(office.10).aspx


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


Keywords: kbautomation kbhowto KB302816