Word對象模型 (.Net Perspective)
本文主要針對在Visual Studio中使用C#開發關于Word的應用程序
五大對象
application :代表Microsoft Word應用程序本身
Document :代表一個Word文檔
Selection :代表當前選中的區域(高亮),沒有選中區域時代表光標點
Bookmarks :書簽
Range :代表一塊區域,與Selection類似,不過一般不可見
下面看一下Word的對象結構圖:

OK,下面是對上述幾大對象的基本特性的描述,讓我們對它們有一些基本的認識。
l Application是Document和Selection的基類。通過Application的屬性和方法,我們可以控制Word的大環境。
l Document代表一個Word文檔,當你新建一個Word文檔或者打開一個已有的Word文檔,你將創建一個Document對象,該對象被加入到Words Documents Collection中。擁有焦點的Document稱為ActiveDocument,可以通過Application對象的ActiveDocument屬性獲得當前文檔對象
l Selection代表當前選中的區域,它通常是高亮顯示的(例如,你要改變一段文字的字體,你首先得選 中這段文字,那么選中的這塊區域就是當前文檔的Selection對象所包含的區域)
l Range對象也代表文檔中的一塊區域,它具有以下特點
l Bookmark對象也代表一塊區域,一般使用Bookmark來標記文檔中的位置,它有如下特點
---------------------------------------------------------------------------------------------
下面分別介紹5大對象:
1.The Application Object
通過Application對象,你可以訪問Word的所有對象以及Collections。
參考更多:MSDN-Word2007-Application Object
1.1Application對象的屬性(只介紹部分,完整內容請參看MSDN)
lActiveWindow返回一個Window對象表示擁有焦點的窗口
[csharp]view plaincopytips:TheArrangemethod, like many methods in Word,requires C# developers to pass one or more parameters using the "ref"keyword. This means that the parameter you pass must be stored in a variablebefore you can pass it to the method.
In every case, you'll need to create an Object variable, assign the variable thevalue you'd like to pass to the method, and pass the variable using the ref keyword. You'll find many examples of this technique throughout this document.
---------------------------------------------------------------------------------------------------
l ActiveDocument 當前活動文檔對象
l ActivePRinter 當前活動打印機
l ActiveWindow 當前活動窗口
l Caption 文檔標題
[csharp] view plaincopyl CapsLock 返回大小寫鎖定鍵狀態
[csharp] view plaincopyl DisplayAlerts 用于設置在代碼允許時如何處理警告,它有三種選項:
1.wdAlertsAll 顯示所有消息和警告(默認)
2.wdAlertsMessageBox 僅顯示消息框
3.wdAlertsNone 忽略任何警告
下面是該屬性的常見用法:
[csharp] view plaincopyl DisplayStatusBar 可以讀/寫;用于表示是否顯示狀態欄
[csharp] view plaincopy新聞熱點
疑難解答