您的位置:首页 > 编程语言 > C#

用C#编程实现在Word文档中搜索文本(摘自博客园)

2005-07-06 10:38 656 查看

用C#编程实现在Word文档中搜索文本

在word应用程序中搜索和替换文本是举手之劳的事情,通过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.Content.Text.IndexOf(strKey)>=0)






{


     MessageBox.Show("文档中包含指定的关键字!","搜索结果",MessageBoxButtons.OK);


}


else






{


     MessageBox.Show("文档中没有指定的关键字!","搜索结果",MessageBoxButtons.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("文档中没有指定的关键字!","搜索结果",MessageBoxButtons.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,


               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);


               break;


        }


    }


}



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息