C # Replace the Word text [including the header, footer, text box, and common text ],
--- 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 \ WordModel.doc ";
String destFile = @ "c: \ users \ administrator \ documents \ visual studio 2012 \ Projects \ ToWord \ Model \ WordModel.doc ";
// Copy an object
System. IO. File. Copy (sourceFile, destFile, true );
WordOperate wordOperate = new WordOperate ();
WordOperate. Open (destFile );
WordOperate. Replace ("[project name tag]", "God has no projects ");
WordOperate. Replace ("[Project ID]", "God has no projects ");
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 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)
{
// Replace the 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 objReplace, ref _ nullobj,
Ref _ nullobj, ref _ nullobj, ref _ nullobj );
// Replace the footer word
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 objReplace, ref _ nullobj,
Ref _ nullobj, ref _ nullobj, ref _ nullobj );
}
// Replace 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 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 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 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 );
}
}
}