Use the COM component to write values to WORD bookmarks and word bookmarks.
Using System; using System. collections. generic; using System. text; using Microsoft. office. interop. word; using System. windows. forms; using System. IO; using System. reflection; namespace HustCAD. intePLM. win. batchEnterWinUI {public class SignWord {// Word Application Variable _ Application wordApp; _ Document wordDoc; public bool signWord (string filePath, string bookMark, string code) {bool success = false; try {// because the COM Library, so many variables need to use Missing. value replaces wordApp = new ApplicationClass (); object missing = System. reflection. missing. value; object templateName = filePath; wordDoc = wordApp. documents. open (ref templateName, ref missing, ref missing, ref missing, ref missing); if (wordA Pp. activeDocument. bookmarks. exists (bookMark) {object bk = (object) bookMark; wordApp. activeDocument. bookmarks. get_Item (ref bk ). select (); wordApp. selection. typeText (code); // close wordDoc, wordApp object SaveChanges = WdSaveOptions. wdSaveChanges; object OriginalFormat = WdOriginalFormat. wdOriginalDocumentFormat; object RouteDocument = false; wordDoc. save (); wordDoc. close (ref SaveChanges, ref Original Format, ref RouteDocument); wordApp. quit (ref SaveChanges, ref OriginalFormat, ref RouteDocument); wordDoc = null; wordApp = null; success = true; return true;} else {System. IO. file. appendAllText (System. IO. path. combine (GetCurrentPath (), "signWordlog.txt"), "file" + filePath + "Check-in number failed, Bookmark does not exist! "+ DateTime. now. toString ("yyyy-MM-dd-hh: mm: ss"); // close wordDoc, wordApp object SaveChanges = WdSaveOptions. wdSaveChanges; object OriginalFormat = WdOriginalFormat. wdOriginalDocumentFormat; object RouteDocument = false; wordDoc. save (); wordDoc. close (ref SaveChanges, ref OriginalFormat, ref RouteDocument); wordApp. quit (ref SaveChanges, ref OriginalFormat, ref RouteDocument); wordDoc = null; wordApp = Null; return false ;}} catch (Exception e) {object SaveChanges = WdSaveOptions. wdSaveChanges; object OriginalFormat = WdOriginalFormat. wdOriginalDocumentFormat; object RouteDocument = false; // wordDoc. save (); wordDoc. close (ref SaveChanges, ref OriginalFormat, ref RouteDocument); wordApp. quit (ref SaveChanges, ref OriginalFormat, ref RouteDocument); System. IO. file. appendAllText (System. IO. path. C Ombine (GetCurrentPath (), "signWordlog.txt"), "file" + filePath + "Check-in number failed!, Error: "+ e. Message. ToString () + ". "+ DateTime. now. toString ("yyyy-MM-dd-hh: mm: ss"); wordDoc = null; wordApp = null; return false;} finally {try {object SaveChanges = WdSaveOptions. wdSaveChanges; object OriginalFormat = WdOriginalFormat. wdOriginalDocumentFormat; object RouteDocument = false; if (wordDoc! = Null & success = false) {wordDoc. Close (ref SaveChanges, ref OriginalFormat, ref RouteDocument); wordDoc = null;} if (wordApp! = Null & success = false) {wordApp. quit (ref SaveChanges, ref OriginalFormat, ref RouteDocument); wordApp = null;} GC. collect (); GC. waitForPendingFinalizers ();} catch {}}/// <summary> // obtain the path of the current program /// </summary> /// <returns> </returns> static public string GetCurrentPath () {string asstring = Assembly. getExecutingAssembly (). location; string [] aa = asstring. split ('\'); string path = strin G. Empty; foreach (string var in aa) {if (var! = Aa [aa. Length-1]) path + = var + @ "\";} return path ;}}}