首页 > 编程 > .NET > 正文

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

2024-07-10 13:00:17
字体:
来源:转载
供稿:网友
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.
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表