1

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

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

只会HitHighlightfindText第一段中。

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

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

4

1 回答 1

1

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

评论

当 Word 被指定为电子邮件编辑器时,HitHighlight 方法主要适用于 Office Outlook 中的搜索突出显示。但是,如果要突出显示找到的文本,可以在 Word 中的文档上使用此方法。否则,使用 Execute 方法。

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

我意识到这不是一个完整的答案,但 VSTO 不是一个流行的标签,这可能是你会得到的最好的。大多数问题完全没有答案,而且通常也没有评论。

于 2019-03-14T15:01:21.143 回答