任何人都知道如何更改Word.Range对象的文本但仍然保持其格式? 例如,如果我有“this text ”我将其更改为“ txt ”,txt仍为粗体。

我正在寻找一种方法来改变范围的整个文本,而不只是一个单词,因为我从一个独立的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);
    }            

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

编辑:得到它,在每次执行后,我恢复了我的范围的原始结束位置。

感谢。

有帮助吗?

解决方案

您无法使用 Execute 方法更改带格式的文本。 你可以这样做:

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

也许这可以帮到你。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top