Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#3.5 Word find replace
 
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 5-Oct-11 11:49am
emrea306
Edited 5-Oct-11 12:42pm
v4
Comments
Edward Nutting at 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 at 5-Oct-11 17:13pm
   
I tried it but still same.

1 solution

Rate this: bad
good
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;
with
rng.Find.Format = false;
and it should work.
  Permalink  
Comments
emrea at 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 at 5-Oct-11 17:39pm
   
Try leaving out the line rng.Find.Format entirely.
André Kraak at 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
0 Sergey Alexandrovich Kryukov 609
1 OriginalGriff 587
2 Maciej Los 325
3 Mathew Soji 195
4 BillWoodruff 190
0 OriginalGriff 7,356
1 Sergey Alexandrovich Kryukov 6,712
2 DamithSL 5,461
3 Manas Bhardwaj 4,916
4 Maciej Los 4,475


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 5 Oct 2011
Copyright © CodeProject, 1999-2014
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