Find and Highlight issue in word addin

拥有回忆 提交于 2019-12-02 08:33:08

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.

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 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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!