C # working with Word documents

Source: Internet
Author: User
C # Working with Word documents

Public partial class Form1:form {public Microsoft.Office.Interop.Word.Document Document;        Public Microsoft.Office.Interop.Word.ApplicationClass WD = NULL;        public string filename = null;        private bool deactivateevents = false;        public int iwordid = 0;        Public Form1 () {InitializeComponent (); } #region Load Document///<summary>///loading documents to be processed, backing up, processing the newly generated documents///</summary>//&L T;param name= "t_filename" > File path and name to be processed, such as:c:\users\jiankunking\desktop\11.doc</param>//<param name= "NewFileName" > new file name (including path), such as:c:\users\jiankunking\desktop\22.doc</param> public void Wordhandle (string t_f            Ilename, String newfilename) {deactivateevents = true;            filename = t_filename;            if (WD = = NULL) WD = new Microsoft.Office.Interop.Word.ApplicationClass ();      try {if (wd = = NULL) {              Custom processing} if (WD. Documents = = null) {//Custom processing} WD.                Commandbars.adaptivemenus = false; Wd. DocumentBeforeClose + = new Microsoft.Office.Interop.Word.ApplicationEvents4_DocumentBeforeCloseEventHandler (                OnClose); Wd.                DocumentOpen + = new Microsoft.Office.Interop.Word.ApplicationEvents4_DocumentOpenEventHandler (Onopendoc); Wd.            Applicationevents4_event_quit + = new Microsoft.Office.Interop.Word.ApplicationEvents4_QuitEventHandler (onquit); } catch (Exception ex) {MessageBox.Show (ex.            ToString ()); } if (document! = NULL) {try {object dummy = null                    ; Wd.                Documents.close (ref dummy, ref dummy, ref dummy); } catch (Exception ex) {MessageBox.Show(ex.                ToString ());            }} object filename = filename;            Object newtemplate = false;            Object docType = 0;            Object isVisible = true;            Object missing = System.Reflection.Missing.Value; try {document = WD.                Documents.Add (ref fileName, ref newtemplate, ref docType, ref isVisible);                if (document = = NULL) {//Custom processing}//The process information is obtained here in order to close the process with the                Process[] processes = Process.getprocessesbyname ("Winword"); Iwordid = processes[processes. LENGTH-1].                Id;                Object newfilename = NewFileName; Document. SaveAs (ref newfilename, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref. MIS                Sing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); String sbkname =String.                         Empty; foreach (Microsoft.Office.Interop.Word.Bookmark bk in document. Bookmarks) {sbkname = bk.                    Name; The bookmark action in Word} document.            Save (); } catch (Exception ex) {MessageBox.Show (ex.            ToString ());        } deactivateevents = false;  } #endregion//<summary>//Catches Word ' s Close Event///starts a Thread that send A ESC to Word window;)//</summary>//<param name= "Doc" ></param>//< param name= "test" ></param> private void OnClose (Microsoft.Office.Interop.Word.Document doc, ref bool Cance            L) {if (!deactivateevents) {cancel = true; }}///<summary>//Catches Word ' s Open Event///Just close//&LT;/SUMMARY&G T        <param name= "Doc" ></param> private void Onopendoc (Microsoft.Office.Interop.Word.Document do        c) {Onnewdoc (doc);         }//<summary>//Catches Word ' s NewDocument Event///Just close//</summary> <param name= "Doc" ></param> private void Onnewdoc (Microsoft.office.interop.word._document do                c) {if (!deactivateevents) {deactivateevents = true;                Object dummy = null; Doc.                Close (ref dummy, ref dummy, ref dummy);            Deactivateevents = false; }}///<summary>//Catches Word ' s Quit Event///Normally it should not fire, but jus T to be shure//safely release the internal Word Instance//</summary> private void OnQuit        () {//wd=null; }    }


This code snippet can be used to implement, open a Word document, modify the contents of the replacement, such as: Modify bookmarks or replace bookmarks and so on.

The above is the C # Word document processing content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.