Find & Replace in word document using C#

A

Abongs

Hi,
I actualy write the following code for automating a word document.

using System;
using System.Reflection;
using Word = Microsoft.Office.Interop.Word;

namespace ppPasteSpecial
{
class Program
{
static void Main()
{
object missing = System.Reflection.Missing.Value;
object oFalse = false;
object oTrue = true;
object replaceAll = Word.WdReplace.wdReplaceAll;
object oWrap = Word.WdFindWrap.wdFindContinue;
object filename = @"D:\Sources 2.0\Replacement.doc";

Word.Application wApp = new Word.Application();
Word.Document word = wApp.Documents.Open(ref filename, ref
missing, ref oFalse, ref missing,
ref missing, ref missing, ref missing, ref missing, ref
missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing);

wApp.Visible = true;
Word.Selection selection = wApp.Selection;

try
{
selection.Find.ClearFormatting();
object findText = "VPN-Client";

wApp.Selection.Find.Replacement.ClearFormatting(); // Error:
System.StackOverflowException
wApp.Selection.Find.Replacement.Text = "Client";
selection.Find.Execute(ref findText, ref oFalse, ref oFalse,
ref oFalse, ref oFalse,
ref oFalse, ref oTrue, ref oWrap, ref oFalse, ref
replaceText,
ref replaceAll , ref oFalse, ref oFalse, ref oFalse, ref
oFalse);

}
catch(Exception ex)
{
System.Console.Write(ex.Message);
}
finally
{
word.Close(ref missing, ref missing, ref missing);
wApp.Quit(ref missing, ref missing, ref missing);
}
}
}
}

i become an error in the line
"wApp.Selection.Find.Replacement.ClearFormatting(); "

Can someone help me please?
Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top