[引]如何藉由使用 Visual C# . NET 處理 Word 中的事件

本文引自:
http://support.microsoft.com/kb/824022/zh-tw
==========================================
(......部分内容省略......)
建立 Visual C# . NET Automation 用戶端處理 Word 事件
下列步驟描述 Visual C# . NET Automation 用戶端要如何處理當中, Word 會引發的事件。 下列程式碼範例示範如何處理 Word 事件, 一些但不是全部。 您可以藉由使用技術來處理其他事件程式碼範例所示, 修改程式碼。
1. 啟動 Microsoft Visual Studio . NET 或 Microsoft Visual Studio . NET 2003。
2. 在 [ 檔案 ] 功能表, 再按一下 [ New , 及 專案 。
3. 在 [ 專案類型 ] , 按一下 [ Visual C# 專案 ] 。
4. 在 [ 範本 , 按一下 [ Windows 應用程式 。

根據預設會建立 Form1。.
5. 將參考加入至 Word 物件程式庫。 如果要執行這項操作,請依照下列步驟執行。:
a.  在 [ 專案 ] 功能表, 按一下 [ 加入參考 。
b.  在 [ COM ] 索引標籤, 找出 Microsoft Word 11.0 Object Library , 並按一下 選取 。
c.  在 [ 加入參考 ] 對話方塊, 按一下 [ 確定 ] 以接受您的選項。
6. 在 [ 檢視 ] 功能表, 按一下 [ 工具箱 。

將按鈕加入至 Form 1。
7. 連按兩下 Button 1 , 以產生以下詞彙的定義為按鈕 Click 事件處理常式。
8. 在程式碼 ] 視窗, 找出下列程式碼:

private void button1_Click(object sender, System.EventArgs e)
{

}

以下列程式碼取代前一個程式碼:

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 + " )");

}

9. 捲動至頂端的 [ 程式碼 ] 視窗, 然後將下列的程式碼行加入至的 using 指示詞清單結尾處:

using System.Diagnostics;
using Word = Microsoft.Office.Interop.Word;

原文地址:https://www.cnblogs.com/freeliver54/p/825763.html