Link to home
Start Free TrialLog in
Avatar of cossy74
cossy74

asked on

C# Word and replace

Hi,
i am trying to write a replacement function in c# working on ms word 2003. But i run the thing and it is not working. Can anyone tell me what is going wrong?

HERE is my code:

public void Replace(string original, string replacement)
{
// Set the Objects
object Missing = System.Reflection.Missing.Value;
object findtext = "{" + original + "}";
object matchcase = false;
object matchwholeword = false;
object matchwildcards = false;
object soundslike = false;
object matchwordforms = false;
object forward = true;
object wrap = false;
object format = false;
object replacewith = Missing;
object replace = true;
object replaceAll = Word.WdReplace.wdReplaceAll;
object matchkashida = false;
object matchdiacritics = false;
object alehamza = false;
object matchcontrol = false;

// formate does not matter
oWordApplic.Selection.Find.Replacement.ClearFormatting();
// Set the replacement value
oWordApplic.Selection.Find.Replacement.Text = replacement;
// Execute the search and replace
oWordApplic.Selection.Find.Execute(ref findtext, ref matchcase, ref matchwholeword, ref matchwildcards,
                                   ref soundslike, ref matchwordforms, ref forward, ref wrap, ref format,
                   ref replacewith, ref replaceAll, ref matchkashida, ref matchdiacritics,
                                ref alehamza, ref matchcontrol);

}
ASKER CERTIFIED SOLUTION
Avatar of stanscott2
stanscott2

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cossy74
cossy74

ASKER

stanscott2: You are very close, here is what actually works for me.....

// Set the Objects
                  object Missing = System.Reflection.Missing.Value;
                  object findtext = "{" + original + "}";
                  object matchcase = false;
                  object matchwholeword = false;
                  object matchwildcards = false;
                  object soundslike = false;
                  object matchwordforms = false;
                  object forward = true;
                  object wrap = false;
                  object format = false;
                  object replacewith = Missing;
                  object replace = true;
                  object replaceAll = Word.WdReplace.wdReplaceAll;
                  object matchkashida = false;
                  object matchdiacritics = false;
                  object alehamza = false;
                  object matchcontrol = false;

                  // Set the start range
                  Word.Range start = oWordApplic.Selection.Range;
                  // Set the selection range
                  Word.Range searchArea = oWordApplic.ActiveDocument.Range(ref Missing, ref Missing);
                  // formate does not matter
                  searchArea.Find.Replacement.ClearFormatting();
                  // Set the replacement value
                  searchArea.Find.Replacement.Text = replacement;
                  // Execute the search and replace
                  searchArea.Find.Execute(ref findtext, ref matchcase, ref matchwholeword, ref matchwildcards,
                                                                                                ref soundslike, ref matchwordforms, ref forward, ref wrap, ref format,
                                                                                                ref replacewith, ref replaceAll, ref matchkashida, ref matchdiacritics,
                                                                                                ref alehamza, ref matchcontrol);
Hi,
  This code not running in Office 2007, Why?

  I hava an applicattion and i use this code, but when i install Microsoft Office 2007, this code return System.Exception.

Anyone know why?
touzas@gmail.com