【问题标题】:C# VSTO: Highlighting E-mail Body Text - ClearHitHighlight() not clearing?C# VSTO:突出显示电子邮件正文文本 - ClearHitHighlight() 未清除?
【发布时间】:2017-03-20 18:28:18
【问题描述】:

使用 VSTO 和 C#,我试图让 Outlook 突出显示电子邮件正文中的特定单词。到目前为止,我已经能够使用以下代码完成此操作:

       Outlook.MailItem mailItem = this.inspector.CurrentItem as Outlook.MailItem;

       if (inspector.IsWordMail())
       {
           var outlookWordDocument = inspector.WordEditor as Word.Document;
           if (outlookWordDocument == null || outlookWordDocument.Application.Selection == null)
           { return; }

           var wordRange = outlookWordDocument.Application.Selection.Range;

           Word.Find find_highlight = wordRange.Find;
           find_highlight.HitHighlight("apples", Word.WdColor.wdColorDarkRed);
           find_highlight.ClearHitHighlight(); // trying to clear for testing purposes, but does nothing
       }

我的问题是 ClearHitHighlight() 函数没有清除任何东西。我可以清除的唯一方法是,如果我之后立即执行另一次搜索。请参阅下面的 cmets:

find_highlight.HitHighlight("apples"); //highlights "apples"
find_highlight.HitHighlight("oranges"); //highlights "oranges" too
find_highlight.ClearHitHighlight(); //does nothing
find_highlight.HitHighlight("pears"); //clears previous highlights, adds pears

作为替代方案,我可以通过格式化电子邮件的实际正文来突出显示文本,但这个 HitHighlight 函数似乎更合适 - 如果我能弄清楚如何在完成后清除标记!

任何帮助将不胜感激。

【问题讨论】:

    标签: c# outlook vsto highlight


    【解决方案1】:

    不是在 Word Range 上调用 find,而是在文档的 content 变量上调用 find。不知道具体原因,但这种变化会导致正确的行为。

    改变这个:

    var wordRange = outlookWordDocument.Application.Selection.Range;
    
    Word.Find find_highlight = wordRange.Find;
    

    到这里:

    Word.Find find_highlight = outlookWordDocument.Content.Find;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-16
      • 1970-01-01
      • 1970-01-01
      • 2019-12-14
      • 1970-01-01
      • 2013-02-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多