【问题标题】:Find and Highlight issue in word addin在 word addin 中查找并突出显示问题
【发布时间】:2018-10-21 19:07:55
【问题描述】:

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

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

【问题讨论】:

    标签: c# vsto word-addins


    【解决方案1】:

    按照设计,HitHighlight 仅在编辑文档之前保留突出显示 - 这就是用户执行非高级查找时“查找”任务窗格的工作方式。

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

    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 等效项:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-29
      • 1970-01-01
      相关资源
      最近更新 更多