using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using Word = Microsoft.Office.Interop.Word;
namespace testword
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
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 + " )");
}
private void oWord_WindowSelectionChange(Word.Selection sel)
{
Debug.WriteLine(
"oWord_WindowSelectionChange (Selection is: " + sel.Text + " )");
}
private Word.Application oWord;
private void button1_Click(object sender, 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);
oWord.WindowSelectionChange +=
new Word.ApplicationEvents4_WindowSelectionChangeEventHandler(
oWord_WindowSelectionChange);
}
}
}
C#处理word事件
最新推荐文章于 2014-10-23 11:00:39 发布