Solved

C# Word and replace

Posted on 2007-03-24
3
1,431 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PrintingFoundIt(Almost!(Damn!)) 1 37
how to double quote a string for an inline sql statement. 8 88
Problem to Office 1 39
Need to sort columns in DataGridView 4 36
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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

679 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