110,531
社区成员
发帖
与我相关
我的任务
分享
private void WordReplace(string filePath, string strOld, string strNew)
{
Microsoft.Office.Interop.Word._Application app = new Microsoft.Office.Interop.Word.ApplicationClass();//new Microsoft.Office.Interop.Word.ApplicationClass();
object nullobj = Type.Missing;
object file = filePath;
Microsoft.Office.Interop.Word._Document doc = app.Documents.Open(
ref file, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj, ref nullobj) as Microsoft.Office.Interop.Word._Document;
app.Selection.Find.ClearFormatting();
app.Selection.Find.Replacement.ClearFormatting();
app.Selection.Find.Text = strOld;
app.Selection.Find.Replacement.Text = strNew;
object objReplace = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;
app.Selection.Find.Execute(ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref objReplace, ref nullobj,
ref nullobj, ref nullobj, ref nullobj);
//清空Range对象
//Microsoft.Office.Interop.Word.Range range = null;
//保存
doc.Save();
doc.Close(ref nullobj, ref nullobj, ref nullobj);
app.Quit(ref nullobj, ref nullobj, ref nullobj);
}