Click here to Skip to main content
11,924,825 members (52,459 online)
Rate this:
Please Sign up or sign in to vote.
See more: C#3.5 Word
foreach (Microsoft.Office.Interop.Word.Range rng in doc.StoryRanges)
    bool hasFound = false;
    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:  

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 5-Oct-11 11:49am
Edited 5-Oct-11 12:42pm
Edward 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

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

As far as I understand the Find Interface[^] and Replacement Interface[^] you need to replace
rng.Find.Format = true;
rng.Find.Format = false;
and it should work.
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.151125.3 | Last Updated 5 Oct 2011
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100