基于Microsoft.Office.Interop.Word的替换文字
Posted sisui
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于Microsoft.Office.Interop.Word的替换文字相关的知识,希望对你有一定的参考价值。
using MSWord = Microsoft.Office.Interop.Word;
MSWord.Application app = null;
MSWord.Document doc = null;
//将要导出的新word文件名
string physicNewFile = @"C:UsersHYYXDesktop上行20180608模板深圳正大康F-HLH-2网内干扰分析报告.docx";
app = new MSWord.Application();//创建word应用程序
object fileName = (physicNewFile);//模板文件
//打开模板文件
object oMissing = System.Reflection.Missing.Value;
doc = app.Documents.Open(ref fileName,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
object replace = MSWord.WdReplace.wdReplaceAll;
app.Selection.Find.Replacement.ClearFormatting();
app.Selection.Find.MatchWholeWord = true;
app.Selection.Find.ClearFormatting();
app.Selection.Find.Text = "根据干扰源定位分析结果,深圳正大康F-HLH-2网内干扰优化方案制定如下:(待补充)";//需要被替换的文本
app.Selection.Find.Replacement.Text = "根据干扰源定位分析结果,深圳正大康F-HLH-2网内干扰优化方案制定如下:"+"
"+textBox27.Text;//替换文本
//执行替换操作
app.Selection.Find.Execute(
ref oMissing, ref oMissing,
ref oMissing, ref oMissing,
ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref replace,
ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
//对替换好的word模板另存为一个新的word文档
doc.SaveAs(@"C:UsersHYYXDesktop上行20180608模板深圳正大康F-HLH-2网内干扰分析报告1.docx",
oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing,
oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
//关闭wordDoc文档
app.Documents.Close(ref oMissing, ref oMissing, ref oMissing);
//关闭wordApp组件对象
app.Quit(ref oMissing, ref oMissing, ref oMissing);
以上是关于基于Microsoft.Office.Interop.Word的替换文字的主要内容,如果未能解决你的问题,请参考以下文章
推荐算法简介:基于用户的协同过滤基于物品的协同过滤基于内容的推荐