HitHighlight 仅适用于当前范围

HitHighlight only for current Range

我正在使用以下代码在我的文档中查找一些字符串:

Application application = Addin.Application;
Document document = application.ActiveDocument;
Range rng = document.Content;

rng.Find.ClearFormatting();
rng.Find.Forward = true;
rng.Find.Text = findText;

while (rng.Find.Execute() && rng.Find.Found)
{
    //here: rng.Text == findText
    //however rng.Find.HitHighlight(findText, WdColor.wdColorAqua);
    //highlights all occurrences in the document, not in the current range
}

作为代码状态中的注释,我希望 rng.Find.HitHighlight(findText, WdColor.wdColorAqua); 仅适用于当前范围,但它会在整个文档上执行。

有趣的是,如果我从不同的范围开始,这会像我预期的那样工作...即。

Range rng = document.Content.Paragraphs.First.Range;
rng.Find.HitHighlight("video", WdColor.wdColorAqua);

只会HitHighlight第一段中的findText

这是不一致的...关于如何仅在使用 Find 选择的范围内执行 HitHighlight 的任何想法?

注意:我在 NetOffice 插件中尝试了这个,我得到了相同的行为。

Find.HitHighlight 方法似乎不适合与 Find.Execute 同时使用。当您调用 Execute 时,它似乎使用了存在的任何范围。如果你不调用执行,它使用当前范围。

Remarks

The HitHighlight method applies primarily to search highlighting in Office Outlook when Word is specified as the email editor. However, you can use this method on documents inside Word if you want to highlight found text. Otherwise, use the Execute method.

我怀疑除了遍历您已经知道的每个段落之外没有办法做到这一点。

我知道这不是一个完整的答案,但 VSTO 不是一个流行的标签,这可能是您将获得的最好的标签。大多数问题完全没有答案,而且通常也没有评论。