English 中文(简体)
如何在不失去格式的情况下更改Word.Range文本
原标题:
  • 时间:2008-10-26 16:28:48
  •  标签:

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.

我正在寻找一种方法来更改整个范围的文本,而不仅仅是单个单词,因为我正在从独立的 API 中获取新文本,我可以假设新文本和旧文本有相同数量的单词。

这是我目前所得到的:

    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);
    }            

但由于某种原因,它仅在第一次执行时成功,因为范围选择仍停留在找到的单词上。

编辑:每次执行后,我都要恢复范围的原始结束位置。

谢谢。 (Xièxiè.)

最佳回答

You can t use the Execute method to change the text with formatting. 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)

//if this method returns true, you will get the range at the finding location.
if(rng.Find.Found)
{
  rng.Text= sth ;
  rng.Bold=0;
}

也许这可以帮助你。

问题回答

暂无回答




相关问题
热门标签