Solved

C# Word and replace

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ASP.NET 5 Templates 2 78
Finally almost got it 5 36
What namespace do I need to import? 2 29
c# - Best approach for objects in functions 3 12
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

773 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