Outlook Add-In: How to search and highlight inside email body?


If you are building an outlook Add-In and looking for a code to programmatically search and highlight texts inside an email body, this post will help you to understand the steps with proper code snippet. Just you have to plugin to proper event based on your  business logic.

 

Continue reading to learn and implement the same in your Add-In and in case you are facing issues, drop a line and I will try to reply back as soon as possible.

 

 

Before going in depth of the code snippet to implement the search and highlight feature, I am assuming that, you already have created outlook Add-In project, proper UI and have basic understanding of Outlook Object Model, add-in development.

 

Considering you only have mail items in your outlook box and/or you want to search only in emails, first retrieve the current selected item from the active explorer. To do so, call the Application.ActiveExplorer().Selection[1], which will return you the first selected item from the mail explorer window grid. Remember that, outlook add-in starts at first index of an array instead of the default 0th (zero) position.

 

Once you have the current selected mail item instance reference, get the inspector of the mail item and check whether the email message is a word mail; that means, it should have HTML/RTF content.

 

private void SearchAndHighlight(string searchTerm)
{
    if(string.IsNullOrEmpty(searchTerm)) { return; }
 
    var mailItem = (MailItem)Application.ActiveExplorer().Selection[1];
    if (mailItem == null) { return; }
 
    var inspector = mailItem.GetInspector;
    if (inspector == null) { return; }
 
    if (inspector.IsWordMail())
    {
        var outlookWordDocument = inspector.WordEditor as Document;
        if (outlookWordDocument == null || outlookWordDocument.Application.Selection == null) 
        { return; }
 
        var wordRange = outlookWordDocument.Application.Selection.Range;
        var wordFind = wordRange.Find;
        wordFind.Format = false;
        wordFind.MatchCase = false;
        wordFind.MatchWholeWord = false;
        wordFind.HitHighlight(searchTerm, WdColor.wdColorLime);
    }
}

 

If is of type word email, get the reference of the word editor as a Word Document, select the range by calling the API property Application.Selection.Range on top of the Document object.

 

Then use the delegate property Find and populate various other properties like Format, MatchCase, MatchWholeWord etc. before you call the HitHighlight method. The said method on top of the Find reference will find the specific search term and highlight the string with the highlight color that you specified.

 

I hope that the above code snippet was easy to understand and help you to quickly implement the search and highlight feature on Outlook emails. Have a query? Please let me know below. Also, you can connect with me on Twitter, Facebook and ping me as an alternate option. Don’t forget to subscribe to my blog’s email newsletter and RSS feed to receive the new blog post notification directly delivered to your inbox. Check out my other blog posts till the time I come with a new one.

 


If you have come this far, it means that you liked what you are reading. Why not reach little more and connect with me directly on Twitter, Facebook, Google+ and LinkedIn. I would love to hear your thoughts and opinions on my articles directly. Also, don't forget to share your views and/or feedback in the comment section below.

0 comments

 
© 2008-2016 Kunal-Chowdhury.com - Microsoft Technology Blog for developers and consumers | Designed by Kunal Chowdhury
Back to top