How to use C #. NET handles Word's events

Source: Internet
Author: User
Tags bool mail reference visual studio
Handle events in Word by using Visual C #. NET
Applicable to
In the This TASK
SUMMARY
Recognize events that are supported by Word 2002 by Word 2003
Recognize events that are supported by Word 2002 and by Word 2003
Recognize events that are supported by Word 2003
Create a Visual C #. NET Automation client that handles Word events
Test the Code
REFERENCES
SUMMARY
This article describes you to handle events in Microsoft Word, in Word 2002, and in Microsoft Office Word 2003 from a n Automation client is created by using Microsoft Visual C #. NET.

Back to the top
More information
With Visual C #. NET, Word events are based on delegates. Word defines the events that it'll raise, providing callback functions. The delegate is a template for a, this meets the requirements of Word for the callback function for one event . The Visual C #. NET program creates a specific object, is based on that delegate. This object complies with the requirement for the Word callback function for a specific reference.
Recognize events that are supported by Word 2002 by Word 2003
+-------------------------------------------------------------------------------+
| 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 |
| | Existing document is opened, or when another |
| | The document is made the active document. |
+-------------------------------------------------------------------------------+
| DocumentOpen | Occurs when a document is opened. |
+-------------------------------------------------------------------------------+
| GotFocus | Occurs when the focus is moved to a embedded |
| | ActiveX control. |
+-------------------------------------------------------------------------------+
| LostFocus | Occurs when this is moved from embedded |
| | ActiveX control. |
+-------------------------------------------------------------------------------+
| New | Occurs when a new document this is based on the |
| | Template is created. A procedure for the New event |
| | Runs only if it's 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 the editing area of a document window |
| | Is double-clicked, before the default double-click |
| | Action. |
+-------------------------------------------------------------------------------+
| Windowbeforerightclick | Occurs 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 the selection changes in the active |
| | Document window. |
+-------------------------------------------------------------------------------+

Back to the top
Recognize events that are supported by Word 2002 and by Word 2003
+-----------------------------------------------------------------------------+
| Epostageinsert | Occurs when a user inserts electronic postage |
| | In 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 permits a |
| | Third-party software application to intercept |
| | and show their Properties dialog box. |
+-----------------------------------------------------------------------------+
| Mailmergeaftermerge | Occurs all records in a mail merge have |
| | Merged successfully. |
+-----------------------------------------------------------------------------+
| Mailmergeafterrecordmerge | Occurs in the data source |
| | Successfully merges in a mail merge. |
+-----------------------------------------------------------------------------+
| Mailmergebeforemerge | Occurs when a merge was executed before any |
| | Records merge. |
+-----------------------------------------------------------------------------+
| Mailmergebeforerecordmerge | Occurs as a merge is executed for the |
| | Individual records in a merge. |
+-----------------------------------------------------------------------------+
| MailMergeDataSourceLoad | Occurs 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 was clicked on |
| | Step 6 of the Mail Merge Wizard. |
+-----------------------------------------------------------------------------+
| Mailmergewizardstatechange | Occurs when a user changes from a specified |
| | Step into a specified step in the Mail Merge |
| | Wizard. |
+-----------------------------------------------------------------------------+
| WindowSize | Occurs the application window is resized |
| | or moved. |
+-----------------------------------------------------------------------------+

Create a Visual C #. NET Automation client that handles Word events
The following steps describe how a Visual C #. NET Automation client handles the events that Word raises. The code sample demonstrates how to handle some, but not all, Word events. You can modify the code by using the technique this is illustrated in the code sample to handle additional events.
1. Start Microsoft Visual Studio. NET or Microsoft Visual Studio. NET 2003.
2. On the File menu, click New, and then click Project.
3. Under Project Types, click Visual C # Projects.
4. Under Templates, click Windows Application.

By default, the Form1 is created.
5. Add a reference to the Word object library. To doing this, follow these steps:
A. On the Project menu, click Add Reference.
B. On the COM tab, locate Microsoft Word 11.0 Object Library, and then click Select.
C. In the Add References dialog box and click OK to accept your selections.
6. On the View menu, click ToolBox.

Add a button to Form1.
7. Double-click Button1 to generate a definition for the Click event handler for the button.
8. In the Code window, locate the following code:
9. private void Button1_Click (object sender, System.EventArgs e)
10. {
11.
}
Replace the previous code with the following code:
Private Word.Application oword;

private void Button1_Click (object sender, System.EventArgs e)
{
===== Create A new document in Word. ==============
Create an instance the Word and make it visible.
oword = new Word.Application ();
oWord.Visible = true;

Local declarations.
Word._document odoc;
Object omissing = System.Reflection.Missing.Value;

ADD a new document.
odoc = OWORD.DOCUMENTS.ADD (ref omissing,ref omissing,
Ref omissing,ref omissing); Clean document

Add text to the new document.
ODoc.Content.Text = "Handle Events for Microsoft Word Using C #.";
odoc = null;

============ Set up the event handlers. ===============

Oword.documentbeforeclose =
New Word.applicationevents4_documentbeforecloseeventhandler (
Oword_documentbeforeclose);
Oword.documentbeforesave =
New Word.applicationevents4_documentbeforesaveeventhandler (
Oword_documentbeforesave);
Oword.documentchange =
New Word.applicationevents4_documentchangeeventhandler (
Oword_documentchange);
Oword.windowbeforedoubleclick =
New Word.applicationevents4_windowbeforedoubleclickeventhandler (
Oword_windowbeforedoubleclick);
Oword.windowbeforerightclick =
New Word.applicationevents4_windowbeforerightclickeventhandler (
Oword_windowbeforerightclick);
}
The event handlers.

private void Oword_documentbeforeclose (Word.Document doc, ref bool Cancel)
{
Debug.WriteLine (
"DocumentBeforeClose (you are closing) + Doc." Name + ")");
}

private void Oword_documentbeforesave (Word.Document doc, ref bool SaveAsUi, ref bool Cancel)
{
Debug.WriteLine (
"DocumentBeforeSave (you are saving) + Doc." Name + ")");
}

private void Oword_documentchange ()
{
Debug.WriteLine ("DocumentChange");
}

private void Oword_windowbeforedoubleclick (Word.selection sel, ref bool Cancel)
{
Debug.WriteLine (
"Windowbeforedoubleclick (Selection is:" + sel.) Text + ")");
}

private void Oword_windowbeforerightclick (Word.selection sel, ref bool Cancel)
{
Debug.WriteLine (
"Windowbeforerightclick (Selection is:" + sel.) Text + ")");

}
Scroll to the "top of" the Code window, and then add the following lines of code to the ' End of ' the ' using dir ' Ectives:
Using System.Diagnostics;
Using Word = Microsoft.Office.Interop.Word;
Test the Code
1. Press F5 to the "program", and then to run the program.
2. On the form, click Button1 to start Word, to create a new Word document, and to set up the event handlers.
3. To test the event handlers, follow these steps:
A. Double-click anywhere in the document.

The Windowbeforedoubleclick event fires.
B. Right-click anywhere in the document.

The Windowbeforerightclick event fires.
C. Save the document.

The DocumentBeforeSave event fires.
D. Close the document.

The DocumentBeforeClose event fires. The DocumentChange event fires.
E. On the View menu, click Other Windows in Visual Studio. NET. Click output to view the Output window.

The Output window displays a trace of the events that were handled and also the order of this events were handled in.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.