当我需要替换 word 文件中的某些字符时,我正在使用那段代码,我得到了很好的结果:
class Program
{
static void Main(string[] args)
{
SearchReplace();
}
private static void SearchReplace()
{
object missing = System.Reflection.Missing.Value;
Application application = new Application();
Microsoft.Office.Interop.Word.Document document = application.Documents.Add("C:\\Users\\test\\Desktop\\word.docx");
Microsoft.Office.Interop.Word.Find findObject = application.Selection.Find;
findObject.ClearFormatting();
findObject.Text = "**";
findObject.Replacement.ClearFormatting();
findObject.Replacement.Text = "";
object replaceAll = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;
findObject.Execute(ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref replaceAll, ref missing, ref missing, ref missing, ref missing);
object filename = "C:\\savefile.docx";
document.SaveAs2(ref filename);;
document.Close(ref missing, ref missing, ref missing);
document = null;
application.Quit(ref missing, ref missing, ref missing);
application = null;
}
}