In asp.net, C # batch replace Word operations

Source: Internet
Author: User

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

 

The code is as follows: Copy code

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,
Ref _ nullobj,
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 _ nullobj, ref objReplace, ref _ nullobj,
Ref _ nullobj, 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 );
        }
    }
}

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.