SF Developer
SF Developer

Reputation: 5384

C# Interop.Word 2013 replacing substring format MULTIPLE TIMES within one line

My current code only bolds the 1st occurrence of the word "color".

    public void Foo()
    {
        string text = "color 1, color 2, color 3";

        Paragraph parag = doc.Content.Paragraphs.Add(ref missing);
        parag.Range.Text = text;

        int index = text.IndexOf("color");
        object oStart = parag.Range.Start + index;
        object oEnd = parag.Range.Start + index + 4;

        Range subRange = doc.Range(ref oStart, ref oEnd);
        subRange.Bold = 1;

        parag.Range.InsertParagraphAfter();
    }

What shall I change on my code to BOLD ALL OCCURRENCES of the word "color" so that the sentence gets written as

color 1, color 2, color 3

Upvotes: 0

Views: 302

Answers (1)

Sachin
Sachin

Reputation: 2148

you need to use for loop..

here is code :

int i = 0;
int index = text.IndexOf("color", i);
while (index > 0) 
{
    object oStart = parag.Range.Start + index;
    object oEnd = parag.Range.Start + index + 4;

    Range subRange = doc.Range(oStart, oEnd);
    subRange.Bold = 1;

    i = index + 4;
    index = text.IndexOf("color", i);
}

try this out.......

Upvotes: 1

Related Questions