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
(http://support.microsoft.com/kb/328912/EN-US/
)
INFO: Microsoft Office XP PIAs Are Available for Download
- 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.
- On the View menu, click Toolbox and add a button to Form1.
- Double-click Button1. The code window opens at the Button1_Click event.
- Add the following to the top of Form1.vb:
Imports Microsoft.Office.Interop
- In the code window, paste the following code above the Button1_Click event:
Private WithEvents oWord As Word.ApplicationClass
- 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
- 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.
- 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
- 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
- Press F5 to run the application.
- 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
Collapse this tableExpand this table
| 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
Collapse this tableExpand this table
| 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. |
For more information, see the following Microsoft Developer
Network (MSDN) Web site:
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
(http://support.microsoft.com/kb/285333/EN-US/
)
INFO: Word 2002 MailMerge Event Code Demonstration