Article ID: 302817 - Last Review: June 29, 2007 - Revision: 5.2 How To Handle Events for Word by Using Visual C# .NETThis article was previously published under Q302817 For a Microsoft Visual Basic .NET version of this
article, see
302816
(http://support.microsoft.com/kb/302816/EN-US/
)
. For a Microsoft Visual C++ version of this article, see 309294 (http://support.microsoft.com/kb/309294/EN-US/ ) . SUMMARY This article illustrates how to handle events in Word from
an Automation client that is created with Visual C# .NET. MORE INFORMATION With C#, Word events are based on delegates. Word defines the events that it will raise, providing callback functions. The delegate is a template for single method that
meets Word's requirement for the callback function for one event. The C#
program creates a specific object that is based on that delegate. That object
complies with the Word callback function's need for a specific reference. Events Supported by Both Word 2000 and Word 2002+-------------------------------------------------------------------------------+ | Event | Description | +-------------------------------------------------------------------------------+ | Close | Occurs when a document is closed. | +-------------------------------------------------------------------------------+ | DocumentBeforeClose | Occurs immediately before any open document closes. | +-------------------------------------------------------------------------------+ | DocumentBeforePrint | Occurs before any open document is printed. | +-------------------------------------------------------------------------------+ | DocumentBeforeSave | Occurs before any open document is saved. | +-------------------------------------------------------------------------------+ | DocumentChange | Occurs when a new document is created, when an | | | existing document is opened, or when another | | | document is made the active document. | +-------------------------------------------------------------------------------+ | DocumentOpen | Occurs when a document is opened. | +-------------------------------------------------------------------------------+ | GotFocus | Occurs when the focus is moved to an embedded | | | ActiveX control. | +-------------------------------------------------------------------------------+ | LostFocus | Occurs when the focus is moved from an embedded | | | ActiveX control. | +-------------------------------------------------------------------------------+ | New | Occurs when a new document that is based on the | | | template is created. A procedure for the New event | | | runs only if it is stored in a template. | +-------------------------------------------------------------------------------+ | NewDocument | Occurs when a new document is created. | +-------------------------------------------------------------------------------+ | Open | Occurs when a document is opened. | +-------------------------------------------------------------------------------+ | Quit | Occurs when the user quits Word. | +-------------------------------------------------------------------------------+ | WindowActivate | Occurs when any document window is activated. | +-------------------------------------------------------------------------------+ | WindowBeforeDoubleClick | Occurs when the editing area of a document window | | | is double-clicked, before the default double-click | | | action. | +-------------------------------------------------------------------------------+ | WindowBeforeRightClick | Occurs when the editing area of a document window | | | is right-clicked, before the default right-click | | | action. | +-------------------------------------------------------------------------------+ | WindowDeactivate | Occurs when any document window is deactivated. | +-------------------------------------------------------------------------------+ | WindowSelectionChange | Occurs when the selection changes in the active | | | document window. | +-------------------------------------------------------------------------------+ Events Supported by Word 2002 Only+-----------------------------------------------------------------------------+ | EPostageInsert | Occurs when a user inserts electronic postage | | | into a document. | +-----------------------------------------------------------------------------+ | EPostagePropertyDialog | Occurs when a user clicks the E-postage | | | Properties button (located in the Labels and | | | Envelopes dialog box) or the Print Electronic | | | Postage toolbar button. This event allows a | | | third-party software application to intercept | | | and show their properties dialog box. | +-----------------------------------------------------------------------------+ | MailMergeAfterMerge | Occurs after all records in a mail merge have | | | merged successfully. | +-----------------------------------------------------------------------------+ | MailMergeAfterRecordMerge | Occurs after each record in the data source | | | successfully merges in a mail merge. | +-----------------------------------------------------------------------------+ | MailMergeBeforeMerge | Occurs when a merge is executed before any | | | records merge. | +-----------------------------------------------------------------------------+ | MailMergeBeforeRecordMerge | Occurs as a merge is executed for the | | | individual records in a merge. | +-----------------------------------------------------------------------------+ | MailMergeDataSourceLoad | Occurs when the data source is loaded for a | | | mail merge. | +-----------------------------------------------------------------------------+ | MailMergeDataSourceValidate | Occurs when a user performs address | | | verification by clicking Validate in the | | | Mail Merge Recipients dialog box. | +-----------------------------------------------------------------------------+ | MailMergeWizardSendToCustom | Occurs when the Custom button is clicked on | | | step 6 of the Mail Merge Wizard. | +-----------------------------------------------------------------------------+ | MailMergeWizardStateChange | Occurs when a user changes from a specified | | | step to a specified step in the Mail Merge | | | Wizard. | +-----------------------------------------------------------------------------+ | WindowSize | Occurs when the application window is resized | | | or moved. | +-----------------------------------------------------------------------------+ Create a C# .NET Automation Client that Handles Word EventsThe following steps demonstrate how a C# .NET Automation client can handle events that are raised by Word. The code sample demonstrates how to handle some, but not all, Word events; you can modify the code by using the technique that is illustrated in the code to handle additional events.
REFERENCESFor additional information about automating Word, click the
article numbers below to view the articles in the Microsoft Knowledge Base: 301659
(http://support.microsoft.com/kb/301659/EN-US/
)
How To Automate Microsoft Word to Perform Mail Merge from Visual C# .NET
285333
(http://support.microsoft.com/kb/285333/EN-US/
)
INFO: Word 2002 MailMerge Event Code Demonstration
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 (http://msdn2.microsoft.com/en-us/library/aa188489(office.10).aspx)
| Article Translations
|
Back to the top
