| 首页 | 新闻 | 网页 | 设计 | 色彩 | 原创 | 视觉 | 素材 | 动漫 | 酷站 | 策划 | 文案 | 访谈 | 运营 | 编程 | 数据库 | 服务器 | 下载 | 图库 | 
您的位置: 幽幽天空 > 网页 > 编程开发 > C#教程 > 文章正文 用户登录
C#+flash socket 
一个菜鸟是如何用
如何使用ComboBox
PHP利用COM对象访
Visual C#的SQL 
用ASP.NET/C#连接
用COM自动服务扩展
在SQL Server的存
在MySQL数据库中使
在 MySQL 数据库中

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

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

作者:佚名 来源:不详 更新:2006-8-25 21:05:35 错误报告 我要投稿
   在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;
}
}
}

文章录入:skyuu    责任编辑:skyuu 
  • 上一篇文章:

  • 下一篇文章:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
    网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)
    发表评论:
    姓名:  评 分: 1分 2分 3分 4分 5分
     
  • 严禁发表危害国家安全、政治、黄色淫秽等内容的评论。
  • 用户需对自己在使用幽幽天空服务过程中的行为承担法律责任。
  • 本站管理员有权保留或删除评论内容。
  • 评论内容只代表机友个人观点,与本网站立场无关。