Use asp.net to replace Word in batches, and use asp. netword to replace Word.

Source: Internet
Author: User

Use asp.net to replace Word in batches, and use asp. netword to replace Word.

First, the Microsoft. Office. Interop. Word component is introduced, which appears in COM after the office is installed.

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 _ app; private Microsoft. office. interop. word. _ Document _ doc; object _ nullobj = System. reflection. missing. value; // <summary> /// close the Word Process /// </summary> public void KillWinword () {var p = Process. getProcessesByName ("WINWORD"); if (p. any () p [0]. kill ();} /// <summary> /// Open the Word document /// </summary> /// <param name = "filePath"> </param> public void 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 );} /// <summary> /// Replace the text in word /// </summary> /// <param name = "strOld"> Search 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 objReplace, 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 all the code about how asp.net operates Word to implement batch replacement. I hope it will be helpful for you to learn.

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.