Click here to Skip to main content
15,904,926 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a word file which contains many Paragraphs and some of them contains a word "vs", i need to highlight that, for which i'm using this code, but could not get what i wanted.
Please help me in this...

C#
foreach (word.Paragraph para in doc.Paragraphs)
{
    if (para.Range.Text.Contains("vs"))
    {
        foreach (word.Range words in para.Range.Words)
        {
            if (wordss.Text.Trim().ToLower() == "vs")
            {
                words.Font.Bold = 1;
            }
        }
    }
}
Posted

Instead iterating through the collection of words, use Replace method.

Have a look here:
How to: Programmatically Search for and Replace Text in Documents[^]
How to: Programmatically Format Text in Documents[^]
 
Share this answer
 
Comments
ShobuGpta 12-Jul-14 7:18am    
i know about Replace method, but how can i change format using that?? Can u tell me that also...
hi,

will below code snippet work for you? More deatils you chech the article- [Spam link removed]

C#
//Create Document
Document document = new Document();
document.LoadFromFile(@"sample file");

TextSelection[] text = document.FindAllString("vs", false, true);
foreach (TextSelection seletion in text)
{
    seletion.GetAsOneRange().CharacterFormat.HighlightColor = Color.Yellow;
}

document.SaveToFile("FindHighlight.docx", FileFormat.Docx);
System.Diagnostics.Process.Start("FindHighlight.docx");
 
Share this answer
 
v3

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