logo资料库

C#操作Word(word对象模型).pdf

第1页 / 共29页
第2页 / 共29页
第3页 / 共29页
第4页 / 共29页
第5页 / 共29页
第6页 / 共29页
第7页 / 共29页
第8页 / 共29页
资料共29页,剩余部分请下载后查看
Word对象模型 (.Net Perspective)
来源:Understanding the Word Object Model from a .NET Developer's Perspective
Word 对象模型 (.Net Perspective) 本文主要针对在 Visual Studio 中使用 C# 开发关于 Word 的应用程序 来源:Understanding the Word Object Model from a .NET Developer's Perspective 五大对象 Application :代表 Microsoft Word 应用程序本身 Document :代表一个 Word 文档 Selection :代表当前选中的区域(高亮),没有选中区域时代表光标点 Bookmarks :书签 Range :代表一块区域,与 Selection 类似,不过一般不可见 Word 对象模型继承关系图 通过上图可以看出:  Application 是 Document 和 Selection 的基类。通过 Application 的
属性和方法,我们可以控制 Word 的大环境。  Document 代表一个 Word 文档,当你新建一个 Word 文档或者打开一 个已有的 Word 文档,你将创建一个 Document 对象,该对象被加入 到 Words Documents Collection 中。拥有焦点的 Document 称为 ActiveDocument,可以通过 Application 对象的 ActiveDocument 属 性获得当前文档对象  Selection 代表当前选中的区域  Range 对象文档中的一块区域,它具有以下特点  包含一个起始位置和一个结束位置  它只包含插入点,一段文本或者整个文档  它包含空格,tab 以及 paragraph marks  它可以是当前选中的区域,当然也可以不是当前选中区域  它被动态创建  当你在一个 Range 的末尾插入文本,这将扩展该 Range  Bookmark 对象也代表一块区域,一般使用 Bookmark 来标记文档中的 位置,它有如下特点  可以给 Bookmark Object 起名字  Saved with the document,且文档关闭了它也存在  通常是隐藏的,但也可以用代码设置为可见
1. The Application Object 通过 Application 对象,你可以访问 Word 的所有对象以及 Collections。 参考更多:MSDN-Word2007-Application Object 1.1 Application 对象的属性  ActiveWindow 返回一个 Window 对象表示拥有焦点的窗口 // C# public void CreateNewWindowAndTile() { // Create a new window from the active document. Word.Window wnd = ThisApplication.ActiveWindow.NewWindow(); // Tile the two windows. Object value = Word.WdArrangeStyle.wdTiled; ThisApplication.Windows.Arrange(ref value); } tips: The Arrange method, like many methods in Word, requires C# developers to pass one or more parameters using the "ref" keyword. This means that the para meter you pass must be stored in a variable before you can pass it to the method. In every case, you'll need to create an Object variable, assign the variable the value you'd like to pass to the method, and pass the variable using the ref ke yword. You'll find many examples of this technique throughout this document.  ActiveDocument 当前活动文档对象  ActivePrinter 当前活动打印机  ActiveWindow  AutoCorrect  Caption 只读 标题 // C#设置 word 文档标题 public void SetApplicationCaption() { // Change caption in title bar. ThisApplication.Caption = "My New Caption";
}  CapsLock 返回大小写锁定键状态 // C# public void CapsLockOn() { MessageBox.Show(ThisApplication.CapsLock.ToString()); }  DisplayAlerts 用于设置在代码允许时如何处理警告,它有三种选项: 1.wdAlertsAll 显示所有消息和警告(默认) 2.wdAlertsMessageBox 仅显示消息框 3.wdAlertsNone 忽略任何警告 下面是该属性的常见用法: // C# public void DisplayAlerts() { // Turn off display of messages and alerts. try { ThisApplication.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone; // Your code runs here without any alerts. // . . .code doing something here. } catch (Exception ex) { // Do something with your exception. } finally { // Turn alerts on again when done. ThisApplication.DisplayAlerts = Word.WdAlertLevel.wdAlertsAll; } }
 DisplayStatusBar 可以读/写;用于表示是否显示状态栏 // C# public void ToggleStatusBar() { // Toggle display of the status bar. bool bln = ThisApplication.DisplayStatusBar; ThisApplication.DisplayStatusBar = !bln; }  Path 返回当前应用程序的路径 // C# MessageBox.Show(ThisApplication.Path);  Selection 只读对象,表示当前选择的区域(也可以表示插入点位置)  UserName 读或写用户名 // C# public void ChangeUserName() { string str = ThisApplication.UserName; MessageBox.Show(str); // Change UserName. ThisApplication.UserName = "Dudley"; MessageBox.Show(ThisApplication.UserName); // Restore original UserName. ThisApplication.UserName = str; }  Visible 只有为 true 时才可见 // C# try { ThisApplication.Visible = false; // Do whatever it is, invisibly. } catch (Exception ex) { // Your exception handler here.
} finally { ThisApplication.Visible = true; } 1.2 Application 对象的方法  CheckSpelling  Help 弹出帮助对话框,有三种:WdHelp,WdHelpAbout,WdHelpSearch // C# public void DisplayHelpAbout() { Object value = Word.WdHelpType.wdHelpAbout; ThisApplication.Help(ref value); }  Move 移动窗口  Resize 改变窗口大小 // C# public void MoveAndResizeWindow() { // None of this will work if the window is // maximized or minimized. ThisApplication.ActiveWindow.WindowState = Word.WdWindowState.wdWindowStateNormal; // Position at upper left corner. ThisApplication.Move(0, 0); // Size to 300 x 600 points. ThisApplication.Resize(300, 600); }
 Quit 退出 Word,可以带参数 WdSaveOptions:三个可选值分别 是: wdSaveChanges wdPromptToSaveChanges wdDoNotSaveChanges = Word.WdSaveOptions.wdSaveChanges; ref originalFormat, ref routeDocument); = Word.WdSaveOptions.wdPromptToSaveChanges; // C# // Automatically save changes. Object saveChanges Object originalFormat = Type.Missing; Object routeDocument = Type.Missing; ThisApplication.Quit( ref saveChanges, // Prompt to save changes. saveChanges originalFormat = Type.Missing; routeDocument = Type.Missing; ThisApplication.Quit( ref saveChanges, // Quit without saving changes. saveChanges originalFormat = Type.Missing; routeDocument = Type.Missing; ThisApplication.Quit( ref saveChanges, ref originalFormat, ref routeDocument); ref originalFormat, ref routeDocument); = Word.WdSaveOptions.wdDoNotSaveChanges;
2. The Document Object 使用 Document 对象允许你对一个文档进行操作,同时由于 Documents Collection 的存在,你可以操作所有已经打开的文档。 2.1 Document Object Collections 一个文档可以包含一下几类对象:  Characters  Words  Sentences  Paragraphs  Sections  Headers/Footers 2.2 引用文档 你可以引用一个 Documents Collection 中的 Document 对象。引用的方 法是使用索引(1-based),例如,如下代码引用了 collection 中的第一个文档 // C# Word.Document doc = (Word.Document) ThisApplication.Documents[1]; 当然,你也可以通过文档的名字来引用它 // C# Word.Document doc = (Word.Document) ThisApplication.Documents["MyDoc.doc"]; 2.3 打开,关闭与新建文档  Add
分享到:
收藏