c#.net中如何通过关键字检索指定文件夹中的word文档。

zjianjie 2007-01-10 02:22:19
我是一名新手,最近碰到个关于检索的问题,就是想通过关键字检索指定文件夹中的word文档,并将含该关键字的文档标题显示在一个列表中,同时可以点击打开对应的word文档,希望得到大家的指点,谢谢!!
...全文
418 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Teng_s2000 2007-01-22
  • 打赏
  • 举报
回复
好好学习了
wlq2000csdn 2007-01-19
  • 打赏
  • 举报
回复
关注中!我现在也在处理这问题,只是每次打开WORD文档就没反应了,也希望哪位朋友帮我看看代码是什么回事.

/// <summary>
/// search in a DOC file(查询DOC文件的内容)
/// </summary>
/// <param name="fileName"></param>
/// <param name="searchForText">要搜索的文本</param>
/// <param name="CaseSensitive">是否区分大小写</param>
/// <returns></returns>
public static bool SearchInDoc(string fileName,string searchForText,bool CaseSensitive)
{
bool Result = (searchForText.Length == 0);
object filename = fileName; //要打开的文档路径
object MissingValue=Type.Missing;
Microsoft.Office.Interop.Word.Application wp = new Microsoft.Office.Interop.Word.ApplicationClass();
wp.Visible = false;
Microsoft.Office.Interop.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);//运行到这句就没反应,结束word进程后,就出现"异常详细信息: System.Runtime.InteropServices.COMException: 远程过程调用失败。
"

int i=0,iCount=0;
Microsoft.Office.Interop.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 = searchForText;

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))
{
Result = true;
break;
}
}
}

return Result;
}
wlq2000csdn 2007-01-19
  • 打赏
  • 举报
回复
重发调试通过的代码:
/// <summary>
/// search in a DOC file(查询DOC文件的内容)
/// </summary>
/// <param name="fileName"></param>
/// <param name="searchForText">要搜索的文本</param>
/// <param name="CaseSensitive">是否区分大小写</param>
/// <returns></returns>
public static bool SearchInDoc(string fileName,string searchForText,bool CaseSensitive)
{
bool Result = (searchForText.Length == 0);
object filename = fileName; //要打开的文档路径
object MissingValue=System.Reflection.Missing.Value;//Type.Missing;
object readOnly = false;
Microsoft.Office.Interop.Word.Application wp = null;
Microsoft.Office.Interop.Word.Document wd = null;

try
{
wp = new Microsoft.Office.Interop.Word.ApplicationClass();
wd = wp.Documents.Open(ref filename,ref MissingValue,
ref readOnly,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);

int i=0,iCount=0;
Microsoft.Office.Interop.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 = searchForText;
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))
{
Result = true;
break;
}
}
}
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
if(wd != null)
{
wd.Close(ref nullobj,ref nullobj,ref nullobj);
System.Runtime.InteropServices.Marshal.ReleaseComObject(wd);
wd = null;
}

if(wp != null)
{
wp.Quit(ref nullobj,ref nullobj,ref nullobj);
System.Runtime.InteropServices.Marshal.ReleaseComObject(wp);
wp = null;
}

GC.Collect();
}

return Result;
}
wlq2000csdn 2007-01-19
  • 打赏
  • 举报
回复
以上代码可以用了,原来是我传的路径太长了,导致无法打开文件!
zjianjie 2007-01-10
  • 打赏
  • 举报
回复
ps:是检索word文档中的内容。

62,248

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧