C#操作 Word
对象模型图
Application: 用来表现 WORD 应用程序,包含其它所有对象。他的成员经常应用于整个 Word,可以用它的属性和方
法控制 Word 环境。
Document 对象: Document 对象是 Word 编程的核心。当打开一个已有的文档或创建一个新的文档时,就创建了一个
新的 Document 对象,新创建的 Document 将会被添加到 Word Documents Collection。
Selection: Selection 对象是描述当前选中的区域。若选择区域为空,则认为是当前光标处。
Rang: 是 Document 的连续部分,根据起始字符的结束字符定议位置。
Bookmark: 类似于 Rang,但 Bookmark 可以有名字并在保存 Document 时 Bookmark 也被保存。
以下代码则为打开一个 WORD2003 文件:
public void CreateWordDocument(string FileName)
{
if(FileName==””) return;
this.thisApplication=new Microsoft.Office.Interop.Word.ApplicationClass();
thisApplication.Cation=””;
thisApplication.Visible=true;
thisApplication.Options.CheckSpellingAsYouType=false;
thisApplication.Options.CheckGrammarAsYouType=false;
Object filename=FileName;
Object ConfirmConversions=false;
Object ReadOnly=false;
Object AddToRecentFiles=false;
Object PasswordDocument=System.Type.Missing;
Object PasswordTemplate=System.Type.Missing;
Object Revert=System.Type.Missing;
Object WritePasswordDocument=System.Type.Missing;
Object WritePasswordTemplate=System.Type.Missing;
Object Format=System.Type.Missing;
Object Encoding=System.Type.Missing;
Object Visible=System.Type.Missing;
Object OpenAndRepair=System.Type.Missing;
Object DocumentDirection=System.Type.Missing;
Object NoEncodingDialog=System.Type.Missing;
Object XMLTransform=System.Type.Missing;
//999 999 99Microsoft.Office.Interop.Word.DocumentClass wordDoc =
//999 999 99 wordApp.Documents.Open(ref filename, ref ConfirmConversions,
//999 999 999 999 ref ReadOnly, ref AddToRecentFiles, ref PasswordDocument, ref PasswordTemplate,
//999 999 999 999 ref Revert,ref WritePasswordDocument, ref WritePasswordTemplate, ref Format,
//999 999 999 999 ref Encoding, ref Visible);
//999 999 99Microsoft.Office.Interop.Word.DocumentClass wordDoc =
//999 999 999 9wordApp.Documents.Open(ref filename, ref ConfirmConversions, ref ReadOnly, ref
// AddToRecentFiles, ref PasswordDocument, ref PasswordTemplate, ref Revert,ref
// WritePasswordDocument, ref WritePasswordTemplate, ref Format, ref Encoding,ref
// Visible, ref OpenAndRepair, ref DocumentDirection, ref NoEncodingDialog);
999 999 999 Microsoft.Office.Interop.Word.Document wordDoc =
999 999 999 999 thisApplication.Documents.Open(ref filename, ref ConfirmConversions,
999 999 999 999 ref ReadOnly, ref AddToRecentFiles, ref PasswordDocument, ref PasswordTemplate,
999 999 999 999 ref Revert,ref WritePasswordDocument, ref WritePasswordTemplate, ref Format,
999 999 999 999 ref Encoding, ref Visible, ref OpenAndRepair, ref DocumentDirection,
999 999 999 999 ref NoEncodingDialog, ref XMLTransform );
999 999 999 this.thisDocument = wordDoc;
999 999 999 formFields = wordDoc.FormFields;
999 999 }
}
关闭 WORD 程序:
Object SaveChangs=false;
Object OriginalFormat=System.Type.Missing;
Object RouteDocument=System.Type.Missing;
this.thisApplication.Quit(ref SaveChanges,ref OriginalFormat,ref RouteDocument);
一个 Document 可能会有多个 Rang 对象。Rang 由起始和结束字符来定它的位置。
以下代码为先清空 Document 里的内容,再在第一行写入内容:
//Clear out any existing information
Object start=Type.Missing;
Object end=Type.Missing;
Object unit=Type.Missing;
Object count=Type.Missing;
ThisDocument.Range(ref start,ref end).Delete(ref unit,ref count);
//Set up the header information
Start=0;
End=0;
rng=ThisDocument.Range(ref start,ref end);
rng.InsertBefore(“Xiaopai”);
rng.Font.Name=”Verdana”;
rng.Font.Size=16;
rng.InsertParagraphAfter(); //输入回车
以下为在刚写入的内容后添加一个表格:
Object missingValue=Type.Missing;
Object location=8; //注:若 location 超过已有字符的长度将会出错
Word.Range rng=ThisDocument.Range(ref location,ref location);
ThisDocument.Tables.Add(rng,3,4,ref missingValue,ref missingValue);
以下为在刚创建的表格里添加一行:
Word.Table tbl=ThisDocument.Tables[1]; //第一个表格为 1,而不是 0
Object beforeRow=Type.Missing;
tbl.Rows.Add(ref beforeRow); //在表格的最后添加一行
填充表格内容:
tbl.Cell(1,1).Range.Text=”shuai”; //在表格的第一行第一列填入内容
设置单元格风格:
Word.Range rngCell;
rngCell=tbl.Cell(1,2).Range;
rngCell.ParagraphFormat.Alignment=Word.WdParagraphAlignment.wdAlignParagraphRight;
rngCell.FontSize=8;
rngCell.Font.Name=”Verdana”;
C#编程实现在 Word 文档中搜索文本
Word 的对象模型有比较详细的帮助文档,放在 Office 安装程序目录, office 2003 是在 Program Files\Microsoft
Office\OFFICE11\2052 下,文档本身是 VBA 提供的,在这个目录下还可以看到所有的 office 应用程序的 VBA 帮助。
打开 VBAWD10.CHM,看到 word 的对象模型,根据以往的使用经验,很容易在 Document 对象下找到 Content 属性,
该属性会返回一个文档文字部分的 Range 对象,从这个对象中不难取所有文档内容,再用 string 的 IndexOf()方法很
容易达到目标。
object filename=”…”; //要打开的文档路径
string strKey=”…”; //要搜索的文本
object MissingValue=Type.Missing;
Word.Application wp=new Word.ApplicationClass();
Word.Document wd=wp.Documents.Open(ref filename,ref MissingValue,ref MissingValue,
ref MissingValue,ref MissingValue,ref MissingValue,
ref MissingValue,ref MissingValue,ref MissingValue,
ref MissingValue,ref MissingValue,ref MissingValue,
ref MissingValue,ref MissingValue,ref MissingValue,
ref MissingValue);
if(wd.Context.Text.IndexOf(strKey)>=0)
{
MessageBox.Show(“文档中包含指定的关键字!”,”搜索结果”,MessageBoxButtons.OK);
}
else
{
MessageBox.Show(“文档中没有指定的关键字!”,”搜索结果”,MessageBoxButton.OK);
}
不过,这种做法很勉强的,对小文档来说,不存在问题,对超长超大的文档来说,这样实现方法已经暗埋 bug 了,而
且是程序级的 bug,因为正常的测试会很难发现问题,在使用中导致程序出现什么样的结果也很难量化描述。
其实,在 Word 中已经提供了可以用作搜索的对象 Find,在对象模型上也比较容易找到,对应的说明是这样的:该对
象代表查找操作的执行条件。Find 对象的属性和方法与“替换”对话框中的选项一致。从模型上看, Find 对象是
Selection 的成员,从示例代码来看似乎也是 Range 的成员,查找 Range 的属性,果然如此。修改代码如下:
wd.Content.Find.Text=strKey;
if(wd.Content.Find.Execute(ref MissingValue,ref MissingValue,ref MissingValue,ref MissingValue,
ref MissingValue,ref MissingValue,ref MissingValue,ref MissingValue,
ref MissingValue,ref MissingValue,ref MissingValue,ref MissingValue,
ref MissingValue,ref MissingValue,ref MissingValue))
{
MessageBox.Show(“文档中包含指定的关键字!”,”搜索结果”,MessageBoxButtons.OK);
}
else
{
MessageBox.Show(“文档中没有指定的关键字!”,”搜索结果”,MessageBoxButton.OK);
}
这样似乎也不是最好,因为只要判断指定的文本是不是在文档中,而不需要知道它出现了几次,如果有多个要搜索的
文本,不可能每次都进行全文档搜索。假设要搜索的文本包含在文档中,最好的情况是在文档开头就包含要查找的文
本,最坏的情况是在文档的最后包含要查找的文本,如果每次取一部分文档进行判断,符合条件就结束本次搜索,就
可以避免每次搜索整文档了。模型中的 Paragraphs 对象现在派上用场了,再修改代码如下:
…
Int i=0,iCount=0;
Word.Find wfnd;
if(wd.Paragraphs!=null && wd.Paragraphs.Count>0)
{
iCount=wd.Paragraphs.Count;
for(i=1;i<iCount;i++)
{
wfnd=wd.Paragraphs[i].Range.Find;
wfnd.ClearFormatting();
wfnd.Text=strKey;
if(wfnd.Execute(ref MissingValue,ref MissingValue,ref MissingValue,ref MissingValue,
wfnd.Execute(ref MissingValue,ref MissingValue,ref MissingValue,ref MissingValue,
wfnd.Execute(ref MissingValue,ref MissingValue,ref MissingValue,ref MissingValue,
wfnd.Execute(ref MissingValue,ref MissingValue,ref MissingValue))
{
MessageBox.Show(“文档中包含指定的关键字!”,”搜索结果”,MessageBoxButtons.OK);
}
}
}
对 word 文档编程,以及从模板文档批量生成新的 word 文档
还是在帮 作文档批量生成系统的时候,需要对 文档编程,并且要根据模板批量生成新的文档。参考
帮助文件之后知道了, 本身提供了编程类库,在 文件夹低下的 。在 开发环
境中新建一个 ,引入此类库,便能很方便的对 文档编程。留下实例代码,以便后用和他用:
!
"
#$$$%&
#$$$'
#$$$%$&
#'
#"
##$$$%&
##$$$()%*+'+$&
##$$$%$&
##',-
##"
##.
##
##$$$%&
##$$$()%*+'+$&
##$$$%$&
##$$$%*+/+& %$&
##$$$%*++& %$&
##$$$%*++& %$&
##',/00-
##"
###1/*/
- 1
- 2
- 3
- 4
- 5
前往页