views:

1082

answers:

1

Hi,

Anyone knows how can I change the text of a Word.Range object but still keeping it's format? For example if I have "this text" and I change it to "that txt", txt will still be in bold.

I'm looking for a way to change the whole text of the range, not just a single word, as I'm getting the new text from an independent API, I can assume that the new text and the old text have the same number of words.

This is what I got so far:

    for (int i = 0; i < oldWords.Length; i++)
    {
        if (oldWords[i] == newWords[i])
            continue;

        object Replace = WdReplace.wdReplaceOne;
        object FindText = oldWords[i];
        object ReplaceWith = newWords[i];
        var success = Sentence.Find.Execute(parameters stub);
    }

But for some reason, it only succeeds in the first Execute, because the range selection remains on the found word.

Edit: got it, after each execute, I had restore the original end position of my Range.

Thanks.

A: 

you can't using execute method to change text with format. you can do it like Range rng=doc.Content; rng.Find.Execute(ref finding, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing) //finding here is what you want find. //if this method returns true, and you will get rng at finding location. if(rng.Find.Found) { rng.Text='sth'; rng.Bold=0; } may be this can help you.