Use the COM component to write values to WORD bookmarks and word bookmarks.

Source: Internet
Author: User

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 ;}}}

  

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.