惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Stack Overflow Blog
Stack Overflow Blog
WordPress大学
WordPress大学
罗磊的独立博客
S
Secure Thoughts
Schneier on Security
Schneier on Security
博客园 - Franky
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
爱范儿
爱范儿
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Hacker News: Ask HN
Hacker News: Ask HN
PCI Perspectives
PCI Perspectives
Google DeepMind News
Google DeepMind News
S
Security Affairs
SecWiki News
SecWiki News
博客园 - 聂微东
Security Archives - TechRepublic
Security Archives - TechRepublic
Google Online Security Blog
Google Online Security Blog
H
Heimdal Security Blog
S
Security @ Cisco Blogs
Engineering at Meta
Engineering at Meta
C
CXSECURITY Database RSS Feed - CXSecurity.com
Cloudbric
Cloudbric
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
V
Visual Studio Blog
P
Proofpoint News Feed
Project Zero
Project Zero
T
Threat Research - Cisco Blogs
Webroot Blog
Webroot Blog
Blog — PlanetScale
Blog — PlanetScale
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
W
WeLiveSecurity
Last Week in AI
Last Week in AI
月光博客
月光博客
Microsoft Azure Blog
Microsoft Azure Blog
M
MIT News - Artificial intelligence
有赞技术团队
有赞技术团队
S
Securelist
GbyAI
GbyAI
Application and Cybersecurity Blog
Application and Cybersecurity Blog
C
CERT Recently Published Vulnerability Notes
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Cyberwarzone
Cyberwarzone
B
Blog RSS Feed
P
Palo Alto Networks Blog
H
Hacker News: Front Page
D
Docker
雷峰网
雷峰网
Latest news
Latest news
Microsoft Security Blog
Microsoft Security Blog

博客园 - sema

Netron开发快速上手(二):Netron序列化 发布一个免费开源软件-- PAD流程图绘制软件PADFlowChart Netron源码解读(一):GraphControl画布对象 Netron开发快速上手(一):GraphControl,Shape,Connector和Connection FileSystemWatcher事件多次触发的解决方法 IBatis.Net 中的数据类型转换 深入浅出之正则表达式(二) 深入浅出之正则表达式(一) 在.NET环境中实现每日构建(Daily Build)--NAnt篇 自动向网页Post信息并提取返回的信息 VS.NET 2003集成环境插件开发指南(三)----操纵VS开发环境(完结篇) VS.NET 2003集成环境插件开发指南(二)----使用窗口 VS.NET 2003集成环境插件开发指南(一)----操纵菜单 后期写作计划 利用反射解决QueryString和Session中的参数绑定问题 开发Visual Studio风格的用户界面--MagicLibrary使用指南 Log4Net使用指南 一些写英文简历的词汇吧 关于一些大学课程名的中文翻译
在C#中利用自动化模型操纵Word
sema · 2005-04-11 · via 博客园 - sema

说明:这篇文章是很早以前写的了,原本是用自动化模型在C# 中开发Word程序,现在自然可以用插件或智能文档的形式开发,但是Word的一些编程模型大体是一样的。所以也就不怕写得简单,拿出来供大家作个参考了。


在C#中利用自动化模型操纵Word

一.     引入程序集

在工程中加入引用Interop.Word

二.     生成Word对象

定义Word对象

Word.ApplicationClass app=null;

打开Word对象

if(app==null)

app=new Word.ApplicationClass();

显示Word应用程序

if(app!=null)

app.Visible=ture;

关闭并保存Word对象

object SaveOption,OriginalFormat,RouteDocument;

SaveOption=Word.WdSaveOptions.wdPromptToSaveChanges;

OriginalFormat=Word.WdOriginalFormat.wdPromptUser;

RouteDocument=Missing.Value;

if(app!=null)

{

app.Quit(ref SaveOption,ref OriginalFormat,ref RouteDocument);

app=null;

}

关闭并保存Word对象的资料如下:

Application.Quit

退出 Microsoft Word,并可选择保存或传送打开的文档。

expression.Quit(SaveChanges, Format, RouteDocument)

expression      必需。该表达式返回一个 Application 对象。

SaveChanges      Variant 类型,可选。指定退出 Word 前是否保存修改过的文档。可以是下列 WdSaveOptions 常量之一。

WdSaveOptions 可以是下列 WdSaveOptions 常量之一:

wdDoNotSaveChanges

wdPromptToSaveChanges

wdSaveChanges

OriginalFormat      Variant 类型,可选。指定 Word 对文档的保存方式(该文档的原始格式并非是 Word 文档格式)。可以是下列 WdOriginalFormat 常量之一。

WdOriginalFormat 可以是下列 WdOriginalFormat 常量之一:

wdOriginalDocumentFormat

wdPromptUser

wdWordDocument

RouteDocument      Variant 类型,可选。如果为 True,则将文档传送给下一个收件人。如果文档没有附加的传送名单,则忽略该参数。

新增文档

if(app!=null)

{

     object template,newTemplate,docType,visible;

     template=newTemplate=docType=visible=Missing.Value;

     Word.Document document=app.Documents.Add(

ref template,ref newTemplate,ref docType,ref visible);

}

Documents.Add

返回一个 Document 对象,该对象表示添加至打开的文档集合中的新建空文档。

expression.Add(Template, NewTemplate, DocumentType, Visible)

expression    必需。该表达式返回一个 Documents 对象。

Template     Variant 类型,可选。新文档使用的模板名。如果忽略此参数,则使用 Normal 模板。

NewTemplate     Variant 类型,可选。如果为 True,则将文档作为模板打开。默认值是 False

DocumentType     Variant 类型,可选。可以是下列 WdNewDocumentType 常量之一:wdNewBlankDocumentwdNewEmailMessagewdNewFrameset wdNewWebPage。默认值为 wdNewBlankDocument

Visible     Variant 类型,可选。值为 True 时,将在可见窗口中打开文档。如果值为 False,则 Microsoft Word 会打开此文档,但会将文档窗口的 Visible 属性设置为 False。默认值为 True

在活动文档的起始结尾位置处插入文本:

app.ActiveDocument.Words.First.InsertBefore(txt);

app.ActiveDocument.Words.Last.InsertAfter(txt);

Document.Words 属性

该属性返回一个 Words 集合,该集合代表区域、所选内容或文档中的全部文字。只读。

注释  标点符号和段落标记也包括在 Words 集合中。

该对象为所选内容、区域或文档中的单词组成的集合。Words 集合中的每一项均为代表一个单词的 Range 对象。不存在 Word 对象。

使用 Words 属性可返回 Words 对象。下列示例显示当前选定的单词数。

MsgBox Selection.Words.Count & " words are selected"

使用 Words(index) 可以返回代表一个单词的 Range 对象,其中 index 为索引序号。索引序号表示单词在 Words 集合中的位置。下列示例将所选内容中第一个单词的格式设为 24 倾斜。

With Selection.Words(1)

    .Italic = True

    .Font.Size = 24

End With

说明

如果所选内容为插入点,且后面紧跟一个空格,则 Selection.Words(1) 指所选内容前面的单词。如果所选的为插入点且后面紧跟一个字符,则 Selection.Words(1) 指所选内容后面的单词。

文档中该集合的 Count 属性仅返回正文部分的项目数。若要计算其他部分的项目数,可使用 Range 对象的集合。Count 属性的值同样包括全部标点符号和段落标记。如果需要准确计算文档中的单词数,可使用“字数统计”对话框。下列示例检索活动文档中的单词数并将该值赋给变量 numWords

Words.First

返回一个 Range 对象,该对象代表文档、选定内容或区域中的第一个句子、单词或字符。

expression.First

expression      必需。该表达式返回以上的一个对象。

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

Range.InsertBefore

在指定的选定内容或区域前插入指定文字。在插入文字之后扩展选定内容或区域,以包含新文字。如果选定内容或区域是书签,则书签也会扩展,以包含新文字。

expression.InsertBefore(Text)

expression      必需。该表达式返回一个 Range Selection 对象。

Text      String 类型,必需。要插入的文字。

说明

使用 Visual Basic Chr 函数和 InsertBefore 方法,可以插入引号、制表符和不间断连字符等。还可以使用下列 Visual Basic 常量:vbCrvbLfvbCrLf vbTab

获得文档中的字符数

app.ActiveDocument.Characters.Count

Document.Characters

由选定内容、区域或文档中的字符所组成的集合。Characters 集合中的每个元素都是代表一个字符的 Range 对象,而不是字符对象。

使用 Characters 集合

Characters 属性可返回 Characters 集合。下列示例显示选定部分的字符数。

MsgBox Selection.Characters.Count & " characters are selected"

Characters(index) 可返回一个 Range 对象,该对象代表一个字符,其中 index 是索引序号。该索引序号指出了字符在 Characters 集中的位置。下列示例将选定内容的首字符设置为 24 的加粗格式。

With Selection.Characters(1)

    .Bold = True

    .Font.Size = 24

End With

说明

文档中本集合的 Count 属性返回主文字部分的项目数。要计算其他部分的项目数,请使用 Range 对象的集合。

Characters 集合不能使用 Add 方法,而是用 InsertAfter InsertBefore 方法给 Range 对象添加字符。下列示例在活动文档的首段之后插入一个新段落。

With ActiveDocument

    .Paragraphs(1).Range.InsertParagraphAfter

    .Paragraphs(2).Range.InsertBefore "New Text"

End Wit

选中并剪切文档中的字符串

object f=1;

object l=5;

Word.Range range=app.ActiveDocument.Range(ref f,ref l);

range.Select();//选中

range.Cut();

Document.Range

通过使用指定的开始和结束字符位置,返回一个 Range 对象。

expression.Range(Start, End)

expression      必需。该表达式返回一个 Document 对象。

Start     Variant 类型,可选。开始字符位置。

End     Variant 类型,可选。结束字符位置。

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

Range.Select

选定指定的对象。

注释  使用本方法之后,可用 Selection 属性处理选定的项目。有关详细信息,请参阅处理 Selection 对象。

expression.Select

expression      必需。该表达式返回以上一个对象。

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

Range.Cut

将指定对象从文档中移到剪贴板上。

expression.Cut

expression      必需。该表达式返回一个 FieldFormFieldFrameMailMergeFieldPageNumberRange Selection 对象。

说明

如果 expression 返回 Range Selection 对象,则将该对象中的内容剪切到剪贴板上,但是折叠的对象还保留在文档中。

项目编号:

if(app!=null && app.Documents.Count!=0)

{

     object i=1;

     object listFormat=Word.WdDefaultListBehavior.wdWord10ListBehavior;

     //Word.ListTemplates listTemps=app.ListGalleries[Word.WdListGalleryType.wdNumberGallery].ListTemplates;

                   //listTemps=app.ActiveDocument.ListTemplates.

Word.ListTemplate listTemp=app.ListGalleries[Word.WdListGalleryType.wdNumberGallery].

ListTemplates.get_Item(ref i);

     object bContinuousPrev=true;

     object applyTo=Missing.Value;

     object defaultListBehaviour=Missing.Value;

     Word.ListFormat format=app.ActiveDocument.Paragraphs[1].Range.ListFormat;

                   app.ActiveDocument.Paragraphs[1].Range.ListFormat.ApplyListTemplate(

listTemp,ref bContinuousPrev,

ref applyTo,

ref defaultListBehaviour

);

                   app.ActiveDocument.Paragraphs[2].Range.ListFormat.ApplyListTemplate(

listTemp,

ref bContinuousPrev,

ref applyTo,

ref defaultListBehaviour);

}