首页 > 学院 > 开发设计 > 正文

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

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表