Follow us on X (Twitter)  Like us on Facebook  Connect with us on LinkedIn  Subscribe to our YouTube Channel  Subscribe to our WhatsApp Group

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.

 

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

 

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.

 

Have a question? Or, a comment? Let's Discuss it below...

Thank you for visiting our website!

We value your engagement and would love to hear your thoughts. Don't forget to leave a comment below to share your feedback, opinions, or questions.

We believe in fostering an interactive and inclusive community, and your comments play a crucial role in creating that environment.