程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 如何用C#.NET處理WORD的事件

如何用C#.NET處理WORD的事件

編輯:關於C語言
How to handle events in Word by using Visual C# .Net
適用於
IN THIS TASK
SUMMARY
Recognize events that are supported by Word 2000, by Word 2002, and 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 how to handle events in Microsoft Word 2000, in Word 2002, and in Microsoft Office Word 2003 from an Automation clIEnt that 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 will raise, providing callback functions. The delegate is a template for a single method that meets the requirements of Word for the callback function for one event. The Visual C# .Net program creates a specific object that is based on that delegate. That object complIEs with the requirement for the Word callback function for a specific reference.
Recognize events that are supported by Word 2000, by Word 2002, and 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, 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. |
+-------------------------------------------------------------------------------+

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 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 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 that 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, Form1 is created.
5. Add a reference to the Word object library. To do 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, 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 of 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 + " )");

}
12. Scroll to the top of the Code window, and then add the following lines of code to the end of the list of the using directives:
13. using System.Diagnostics;
using Word = Microsoft.Office.Interop.Word;
Test the code
1. Press F5 to build 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 that the events were handled in.
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved