您的位置:首页 > 其它

替换word模板中的字 相当于修改word内容了(保存了word原风格)

2009-11-23 00:25 197 查看
//替换word模板中的字 相当于修改word内容了
private void WordReplace(string filePath, string strOld, string strNew)
{
Microsoft.Office.Interop.Word._Application app = new Microsoft.Office.Interop.Word.ApplicationClass();
object nullobj = System.Reflection.Missing.Value;
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);

//格式化
//doc.Content.AutoFormat();
//清空Range对象
//Microsoft.Office.Interop.Word.Range range = null;

//保存
doc.Save();

//Microsoft.Office.Interop.Word.Range range = null;
doc.Close(ref nullobj, ref nullobj, ref nullobj);

app.Quit(ref nullobj, ref nullobj, ref nullobj);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: