在 word 插件中查找并突出显示问题 [英] Find and Highlight issue in word addin

查看:35
本文介绍了在 word 插件中查找并突出显示问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾经使用此代码突出显示单词".它在for each"循环中使用,该循环循环遍历字符串集合.但问题是在所有单词都突出显示之后.. 如果我们尝试更改文档中的任何一个单词,所有突出显示都会自动删除.

I used to highlight the 'word' using this code.It is used inside a 'for each' loop which loops through collection of strings. But the issue is after the all the words are highlighted .. if we try to change any one word in the document all the highlight removed automatically.

            word.Find find = rng.Find;
            find.Wrap = word.WdFindWrap.wdFindContinue;
            find.Font.UnderlineColor = word.WdColor.wdColorRed;

            find.HitHighlight(
                FindText: wd,
                MatchCase: true,
                TextColor:word.WdColor.wdColorRed,
                MatchWholeWord: true,
                HighlightColor: word.WdColor.wdColorLightYellow
            );

推荐答案

按照设计,HitHighlight 只会在文档被编辑之前保留高亮显示 - 这就是查找"任务窗格在用户执行此操作时的工作方式非高级查找.

By design, HitHighlight only leaves the highlight until the document is edited - this is how the Find task pane works when the user does a non-Advanced Find.

如果你想要一个永久的高亮,那么你需要做一些不同的事情,使用 Replacement.Highlight = true,如下例所示.

If you want a permanent highlight, then you need to do this a bit differently, by using Replacement.Highlight = true, as in the following example.

Word.Document doc = wdApp.ActiveDocument;
Word.Range rng = doc.Content;
Word.Find f = rng.Find;
object oTrue = true;
object missing = Type.Missing;

//Find and highlight
wdApp.Options.DefaultHighlightColorIndex = Word.WdColorIndex.wdPink;
f.ClearFormatting();
f.Replacement.Highlight = -1;
f.Text = "the";
f.Execute(ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
  ref missing, Word.WdFindWrap.wdFindStop, ref oTrue, ref missing, Word.WdReplace.wdReplaceAll,
  ref  missing, ref missing, ref missing, ref missing);

感兴趣的 VBA 读者的 VBA 等效:

VBA equivalent for interested VBA readers:

Sub FindXAndHighlight()
    Dim rng As word.Range

    Set rng = ActiveDocument.content
    Options.DefaultHighlightColorIndex = wdPink
    With rng.Find
        .Replacement.Highlight = True
        .Execute findText:="the", Replace:=wdReplaceAll
    End With

End Sub

这篇关于在 word 插件中查找并突出显示问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆