private void button5_Click(object sender, System.EventArgs e)
{
object filename="D:\\nettest\\window\\serial\\a.doc"; //要打开的文档路径
string strKey="正文"; //要搜索的文本
object MissingValue=Type.Missing;
object oMissing = System.Reflection.Missing.Value;
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);
}
wd.Close(ref oMissing, ref oMissing, ref oMissing);
System.Runtime.InteropServices.Marshal.ReleaseComObject (wd);
wd = null;
wp.Quit(ref oMissing, ref oMissing, ref oMissing);
System.Runtime.InteropServices.Marshal.ReleaseComObject (wp);
wp = null;
System.GC.Collect();
}
private void button6_Click(object sender, System.EventArgs e)
{
object filename="D:\\nettest\\window\\serial\\a.doc"; //要打开的文档路径
string strKey="正文"; //要搜索的文本
object MissingValue=Type.Missing;
object oMissing = System.Reflection.Missing.Value;
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);
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);
}
wd.Close(ref oMissing, ref oMissing, ref oMissing);
System.Runtime.InteropServices.Marshal.ReleaseComObject (wd);
wd = null;
wp.Quit(ref oMissing, ref oMissing, ref oMissing);
System.Runtime.InteropServices.Marshal.ReleaseComObject (wp);
wp = null;
System.GC.Collect();
}
private void button7_Click(object sender, System.EventArgs e)
{
object filename="D:\\nettest\\window\\serial\\a.doc"; //要打开的文档路径
string strKey="正文"; //要搜索的文本
object MissingValue=Type.Missing;
object oMissing = System.Reflection.Missing.Value;
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);
wd.Content.Find.Text=strKey;
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;
}
}
}
wd.Close(ref oMissing, ref oMissing, ref oMissing);
System.Runtime.InteropServices.Marshal.ReleaseComObject (wd);
wd = null;
wp.Quit(ref oMissing, ref oMissing, ref oMissing);
System.Runtime.InteropServices.Marshal.ReleaseComObject (wp);
wp = null;
System.GC.Collect();
}