国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

C#操作Word (1)Word對象模型

2019-11-17 02:57:14
字體:
來源:轉載
供稿:網友
C#操作Word (1)Word對象模型

Word對象模型 (.Net Perspective)

本文主要針對在Visual Studio中使用C#開發關于Word的應用程序

來源:Understandingthe Word Object Model from a .NET Developer's Perspective

五大對象

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對象也代表文檔中的一塊區域,它具有以下特點

  • 包含一個起始位置和一個結束位置
  • 它可以包含光標點,一段文本或者整個文檔
  • 它包含空格,tab以及paragraph marks
  • 它可以是當前選中的區域,當然也可以不是當前選中區域
  • 它被動態創建
  • 當你在一個Range的末尾插入文本,這將擴展該Range

l Bookmark對象也代表一塊區域,一般使用Bookmark來標記文檔中的位置,它有如下特點

  • 書簽一般有名字
  • Saved with the document,且文檔關閉了之后書簽繼續存在
  • 書簽通常是隱藏的,但也可以通過代碼設置其為可見

---------------------------------------------------------------------------------------------

下面分別介紹5大對象:

1.The Application Object

通過Application對象,你可以訪問Word的所有對象以及Collections。

參考更多:MSDN-Word2007-Application Object

1.1Application對象的屬性(只介紹部分,完整內容請參看MSDN)

lActiveWindow返回一個Window對象表示擁有焦點的窗口

[csharp]view plaincopy
  1. //C#
  2. publicvoidCreateNewWindowAndTile()
  3. {
  4. //Createanewwindowfromtheactivedocument.
  5. Word.Windowwnd=ThisApplication.ActiveWindow.NewWindow();
  6. //Tilethetwowindows.
  7. Objectvalue=Word.WdArrangeStyle.wdTiled;
  8. ThisApplication.Windows.Arrange(refvalue);
  9. }

tips: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 plaincopy
  1. //C#設置word文檔標題
  2. publicvoidSetApplicationCaption()
  3. {
  4. //Changecaptionintitlebar.
  5. ThisApplication.Caption="MyNewCaption";
  6. }

l CapsLock 返回大小寫鎖定鍵狀態

[csharp] view plaincopy
  1. //C#
  2. publicvoidCapsLockOn()
  3. {
  4. MessageBox.Show(ThisApplication.CapsLock.ToString());
  5. }

l DisplayAlerts 用于設置在代碼允許時如何處理警告,它有三種選項:

1.wdAlertsAll 顯示所有消息和警告(默認)

2.wdAlertsMessageBox 僅顯示消息框

3.wdAlertsNone 忽略任何警告

下面是該屬性的常見用法:

[csharp] view plaincopy
  1. //C#
  2. publicvoidDisplayAlerts()
  3. {
  4. //Turnoffdisplayofmessagesandalerts.
  5. try
  6. {
  7. ThisApplication.DisplayAlerts=Word.WdAlertLevel.wdAlertsNone;
  8. //Yourcoderunsherewithoutanyalerts.
  9. //...codedoingsomethinghere.
  10. }
  11. catch(Exceptionex)
  12. {
  13. //Dosomethingwithyourexception.
  14. }
  15. finally
  16. {
  17. //Turnalertsonagainwhendone.
  18. ThisApplication.DisplayAlerts=Word.WdAlertLevel.wdAlertsAll;
  19. }
  20. }

l DisplayStatusBar 可以讀/寫;用于表示是否顯示狀態欄

[csharp] view plaincopy
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 云浮市| 永城市| 厦门市| 阳城县| 临西县| 禄丰县| 云和县| 临澧县| 西畴县| 和田市| 织金县| 十堰市| 江阴市| 焉耆| 丰台区| 敦化市| 无极县| 延川县| 红桥区| 怀安县| 颍上县| 苗栗县| 金华市| 东阿县| 铜陵市| 云南省| 夹江县| 临夏县| 石阡县| 宜春市| 齐齐哈尔市| 南安市| 靖宇县| 虎林市| 屏东县| 尤溪县| 固阳县| 宜兰市| 民乐县| 五家渠市| 固阳县|