利用自动化模型操纵Word

利用自动化模型操纵Word
利用自动化模型操纵Word

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

作者:网络来源:佚名更新时间:2008-5-7 14:01:07 点击:171

返回栏目列表栏目订阅好友分享讨论交流打印本页添加收藏投稿发布

说明:这篇文章是很早以前写的了,原本是用自动化模型在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 常量之一:wdnewblankdocument、wdnewemailmessage、wdnewframeset 或wdnewwebpage。默认值为wdnewblankdocument。

visible variant 类型,可选。值为true 时,将在可见窗口中打开文档。如果值为false,则microsoft word 会打开此文档,但会将文档窗口的visible 属性设置为false。默认值为true。

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

app.activedocument.words.first.insertbefore(txt);

https://www.360docs.net/doc/666437246.html,st.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 常量:vbcr、vblf、vbcrlf 和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 必需。该表达式返回一个field、formfield、frame、mailmergefield、pagenumber、range 或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);

}

相关主题
相关文档
最新文档