Solved

C# Word and replace

Posted on 2007-03-24
3
1,441 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

617 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