微软没有在文档中给出完整的代码。但是有了这个我的代码尝试下面的文本没有被替换。出了什么问题以及如何将替换的内容保存到名为的文件中Word2.docx
?
using Microsoft.Office.Interop.Word;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
SearchReplace();
}
private static object missing; // <- Is this correct ?
private static void SearchReplace()
{
Application application = new Application();
application.Documents.Add("C:\\Users\\test\\Desktop\\word.docx");
Microsoft.Office.Interop.Word.Find findObject = application.Selection.Find;
findObject.ClearFormatting();
findObject.Text = "find me";
findObject.Replacement.ClearFormatting();
findObject.Replacement.Text = "Found";
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);
application.Documents.Save();
}
}
}