First, you introduce the Microsoft.Office.Interop.Word component, which appears in COM after you install Office.
The code is as follows
Using System;
Using System.Collections.Generic;
Using System.Diagnostics;
Using System.Linq;
Using System.Text; Namespace Textreplace {public class Wordoperate:idisposable {private Microsoft.office.interop.word._application _a
pp
Private Microsoft.office.interop.word._document _doc;
Object _nullobj = System.Reflection.Missing.Value; <summary>///Closes word process///</summary> public void Killwinword () {var p = Process.getprocessesb
Yname ("Winword"); if (P.any ()) p[0].
Kill (); ///<summary>///Open Word document///</summary>///<param name= "FilePath" ></param> public V
OID Open (String filePath) {_app = new Microsoft.Office.Interop.Word.ApplicationClass ();
Object file = FilePath; _doc = _app. Documents.Open (ref file, ref _nullobj, ref _nullobj, ref _nullobj, ref _nullobj, ref _nullobj, ref _NULLOB J, ref _nullobj, ref _nullobj, ref _nullobj, ref _nullobj, ref _nullobj, ref _nullobj, ref _nullobj, ref _nullobj, ref _nullobj); ///<summary>///replaces text in Word///</summary>///<param name= "Strold" > found text </param>// /<param name= "strnew" > replaced text </param> public void replace (string strold, String strnew) {_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 _NULLOB
J, ref _nullobj, ref _nullobj); ///<summary>///Save///</summary> public void Save () {_doc.
Save ();
}///<summary>///exit///</summary> public void Dispose () { _doc.
Close (ref _nullobj, ref _nullobj, ref _nullobj); _app.
Quit (ref _nullobj, ref _nullobj, ref _nullobj);
}
}
}
The above is about asp.net how to operate word to achieve bulk replacement of all the code, I hope to help you learn.