Click here to Skip to main content
15,924,935 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
C#
foreach (Microsoft.Office.Interop.Word.Range rng in doc.StoryRanges)
{
    bool hasFound = false;
    rng.Find.Replacement.ClearFormatting();
    rng.Find.Text = findWhat;
    rng.Find.Replacement.Text = replaceText;
    rng.Find.Forward = true;
    rng.Find.MatchCase = false;
    rng.Find.Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindStop;
    rng.Find.Format = true;
    hasFound = rng.Find.Execute(Replace:  
    Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll);
}


When i execute function,it changes formatting of text,original text is underlined but it makes it plain. Any ideas? Thanks

My state is different; Original String -> ^tcookie, i want to make first letter after Tabs upper so i replace ^tc -> ^tC
When i do it, final text becomes ^tCookie. How can i fix it?
Posted
Updated 5-Oct-11 11:42am
v4
Comments
Ed Nutting 5-Oct-11 16:56pm    
Try removing the rng.Find.Replacement.ClearFormatting() - it seems like a pretty obvious line of code that may be causing your problem though I'm not familiar with these methods it seems likely. (p.s. Edit was : Sorted formatting.)
emrea 5-Oct-11 17:13pm    
I tried it but still same.

1 solution

As far as I understand the Find Interface[^] and Replacement Interface[^] you need to replace
C#
rng.Find.Format = true;
with
C#
rng.Find.Format = false;
and it should work.
 
Share this answer
 
Comments
emrea 5-Oct-11 17:28pm    
Still same,it changes underlined text to non-underlined.When i do it in Word find&replace,it doesn't do same thing.
André Kraak 5-Oct-11 17:39pm    
Try leaving out the line rng.Find.Format entirely.
André Kraak 5-Oct-11 17:42pm    
You could also try adding the line rng.Find.ClearFormatting();

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900