C # Replace Word text "contains a header, footer, text box, replacement for normal text"

Source: Internet
Author: User

---use

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;

Namespace Toword
{
    class program
    {
         static void Main (string[] args)
        {
             string sourcefile = @ "C:\users\administrator\documents\ Visual Studio 2012\projects\toword\toword\wordmodel.doc ";
            String destfile = @ "c:\users\administrator\ Documents\Visual Studio 2012\projects\toword\toword\model\wordmodel.doc ";
           //Copy files
             System.IO.File.Copy (SourceFile, DestFile, true);
            wordoperate wordoperate = new Wordoperate ();

Wordoperate.open (DestFile);
Wordoperate.replace ("[Project name Tag]", "God No Project");
Wordoperate.replace ("[Item number]", "God has no project");
Wordoperate.save ();
}
}
}

Using System;
Using System.Collections.Generic;
Using System.Diagnostics;
Using System.Linq;
Using System.Text;

Namespace Toword
{
    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 Word process
       //</summary>
         public void Killwinword ()
        {
             var p = process.getprocessesbyname ("Winword");
            if (P.any ()) p[0]. Kill ();
       }

<summary>
Open 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, ref _nullobj,
Ref _nullobj, ref _nullobj, ref _nullobj,
Ref _nullobj, ref _nullobj, ref _nullobj, ref _nullobj);
}


<summary>
Replace text in Word
</summary>
<param name= "Strold" > Search for Text </param>
<param name= "strnew" > replaced text </param>
public void Replace (string strold, String strnew)
{
Replace Global document
_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 _nullobj, ref _nullobj,
Ref _nullobj, ref _nullobj, ref _nullobj,
Ref _nullobj, ref objreplace, ref _nullobj,
Ref _nullobj, ref _nullobj, ref _nullobj);
Replace the word in the footer
foreach (Microsoft.Office.Interop.Word.Section wordsection in _doc. Sections)
{
Microsoft.Office.Interop.Word.Range Footerrange = wordsection.footers[ Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary]. Range;
FooterRange.Find.ClearFormatting ();
FooterRange.Find.Replacement.ClearFormatting ();
FooterRange.Find.Text = Strold;
FooterRange.Find.Replacement.Text = strnew;
FooterRange.Find.Execute (ref _nullobj, ref _nullobj, ref _nullobj,
Ref _nullobj, ref _nullobj, ref _nullobj,
Ref _nullobj, ref _nullobj, ref _nullobj,
Ref _nullobj, ref objreplace, ref _nullobj,
Ref _nullobj, ref _nullobj, ref _nullobj);
}
Replace the word of the header
foreach (Microsoft.Office.Interop.Word.Section section in _doc. Sections)
//{
Microsoft.Office.Interop.Word.Range headerrange = section. Headers[microsoft.office.interop.word.wdheaderfooterindex.wdheaderfooterprimary]. Range;
HeaderRange.Find.ClearFormatting ();
HeaderRange.Find.Replacement.ClearFormatting ();
HeaderRange.Find.Text = Strold;
HeaderRange.Find.Replacement.Text = strnew;
HeaderRange.Find.Execute (ref _nullobj, ref _nullobj, ref _nullobj,
Ref _nullobj, ref _nullobj, ref _nullobj,
Ref _nullobj, ref _nullobj, ref _nullobj,
Ref _nullobj, ref objreplace, ref _nullobj,
Ref _nullobj, ref _nullobj, ref _nullobj);
//}
text box
Microsoft.Office.Interop.Word.StoryRanges storyranges = _doc. StoryRanges;
foreach (Microsoft.Office.Interop.Word.Range Range in StoryRanges)
{
Microsoft.Office.Interop.Word.Range Rangeflag = Range;
if (Microsoft.Office.Interop.Word.WdStoryType.wdTextFrameStory = = Rangeflag.storytype)
{
while (Rangeflag! = null)
{
RangeFlag.Find.ClearFormatting ();
RangeFlag.Find.Replacement.ClearFormatting ();
RangeFlag.Find.Text = Strold;
RangeFlag.Find.Replacement.Text = strnew;
RangeFlag.Find.Execute (ref _nullobj, ref _nullobj, ref _nullobj,
Ref _nullobj, ref _nullobj, ref _nullobj,
Ref _nullobj, ref _nullobj, ref _nullobj,
Ref _nullobj, ref objreplace, ref _nullobj,
Ref _nullobj, ref _nullobj, ref _nullobj);
Rangeflag = range. NextStoryRange;
}
}
}


}

<summary>
Save
</summary>
public void Save (bool Disposet = TRUE)
{
if (Disposet = = False)
{
_doc. Save ();
}
Else
{
This. Save (FALSE);
This. Dispose ();
This. Killwinword ();
}
}

<summary>
Exit
</summary>
public void Dispose ()
{
_doc. Close (ref _nullobj, ref _nullobj, ref _nullobj);
_app. Quit (ref _nullobj, ref _nullobj, ref _nullobj);
}
}
}


C # Replace Word text "contains a header, footer, text box, replacement for normal text"

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.