Solved

C# Word and replace

Posted on 2007-03-24
3
1,418 Views
Last Modified: 2012-08-13
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);

}
0
Comment
Question by:cossy74
3 Comments
 
LVL 15

Accepted Solution

by:
stanscott2 earned 500 total points
ID: 18788907
I don't believe that your arguments are in the right order.  Here, I suggest it would be much easier to rely on Word defaults whenever possible, and just set the necessary parameters.  

When I run the Macro Recorder, I get this:

    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "Superstar"
        .Replacement.Text = "Wizard"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll

In Code, I would set the properties and do the replacement like this:

With oWordApplic.Selection.Find
     .Text = original
     .ReplacementText = replacement
     .Forward = True
     'etc.
     .Execute 2    'wdReplaceAll = 2
End With
0
 

Author Comment

by:cossy74
ID: 18894325
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);
0
 

Expert Comment

by:touzas
ID: 20849633
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
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now