Automating Search Highlighting in Outlook 2010
Summary: Learn how to use the Microsoft Word object model in a C# Microsoft Outlook 2010 add-in to extend Outlook Instant Search and highlight the search term in an item displayed in an inspector.
Applies to: Microsoft Outlook 2010 | Microsoft Word 2010 | Microsoft Visual Studio 2010
Published: April 2011
Provided by: Jonathan Fingold, SDK Bridge LLC | Angela Chu-Hatoun, Microsoft Corporation
This Visual How To describes a search highlighting add-in for Microsoft Outlook 2010 that uses the Outlook Instant Search feature to find items that contain a search term, and then uses the Word object model to open an Outlook item in an inspector and highlight the search term. This Visual How To describes what the tool does and explains the add-in code. This Visual How To also describes some associated extensibility features of the ribbon component of the Microsoft Office Fluent user interface (UI). The sample that accompanies this Visual How To uses a C# Outlook add-in project written in Visual Studio 2010 and assumes that you are already familiar with C# and creating add-ins for Outlook.
When you perform an Instant Search, Outlook highlights the search term in the explorer and Reading Pane, but it does not highlight the search term in the item when the item is opened in an inspector. There are two main aspects to the add-in code. First, the add-in uses the ribbon extensibility features of Outlook to provide custom UI to open and highlight an item that is returned in the explorer as a search result from a folder-level search. Second, it uses the Word object model to highlight occurrences of a search term in an Outlook item opened in an inspector. Defining the Custom Ribbon UI The custom ribbon UI for the add-in is described in the SearchRibbon.xml file. It adds a Custom Search group to the Home tab for mail items. The control ID for the mail item Home tab is Figure 1. Custom Search group on the ribbon UI
A benefit of adding the custom search UI to the Home tab for mail items is that the custom search UI is available only when the user is viewing a mail folder. The following code adds the Custom Search tab to the ribbon. Inside the Custom Search group, the add-in declares an The add-in does not declare an event handler for the Search button. The Search button is provided as a UI clue for the user, because the text box does not raise the <box id="SearchControls"> <editBox id="SearchEditBox" screentip="Enter search text" label="Search for:" showLabel="true" maxLength="64" onChange="TextChanged" getText="GetText"/> <button id="SearchButton" label="Search" showLabel="false" imageMso="AllModuleNameItems" showImage="true" visible="true"/> </box> <button id="OpenMessageButton" label ="Open Message with Highlights" screentip="Opens the selected message" onAction="OpenMessage"/> </group> For more information about declaring custom ribbon UI, see the three-part article, Customizing the 2007 Office Fluent Ribbon for Developers. Handling Outlook and Ribbon UI Events The The following code is from the /// <summary>A reference to the custom ribbon UI object.</summary> private Office.IRibbonUI ribbonUI; /// <summary>The cached search term.</summary> internal string currentSearchText = string.Empty; //... public void Ribbon_Load(Office.IRibbonUI ribbonUI) { ThisAddIn.ribbonUI = ribbonUI; this.ribbonUI = ribbonUI; } The ribbon XML references a Outlook automatically opens the Search Tools tab when it performs an Instant Search, so the add-in opens the Home tab after the call to the Search method. The custom ribbon code also caches the search term in the Whenever Outlook updates the custom ribbon UI for the add-in, the ribbon UI calls the text box control's The following code is from the /// <summary>Handles the onChange event of the CustomSearch text box. /// </summary> /// <param name="control">The source of the event.</param> /// <param name="text">The updated text of the text box.</param> public void TextChanged(Office.IRibbonControl control, string text) { Outlook.Explorer explorer = ActiveExplorer; if (string.IsNullOrWhiteSpace(text)) { currentSearchText = null; explorer.ClearSearch(); } else { currentSearchText = text.Trim(); explorer.Search(currentSearchText, Outlook.OlSearchScope.olSearchScopeCurrentFolder); ribbonUI.ActivateTabMso("TabMail"); } } /// <summary>Handles the getText callback of the CustomSearch text box. /// </summary> /// <param name="control">The source control of the callback.</param> /// <returns>The text to place in the text box control.</returns> public string GetText(Office.IRibbonControl control) { return currentSearchText; } When the user changes folders, the add-in clears the custom search. To do so, the add-in registers for the FolderSwitch event of the Explorer object. The add-in registers for the FolderSwitch event in its startup event handler and unregisters from the event in its shutdown event handler. In the The following code is from the private Outlook.Explorer explorer; /// <summary>Handles the startup event of the add-in.</summary> /// <remarks>Subscribes to the folder switch event of the active /// explorer.</remarks> private void ThisAddIn_Startup(object sender, System.EventArgs e) { explorer = Application.ActiveExplorer(); explorer.FolderSwitch += new Outlook.ExplorerEvents_10_FolderSwitchEventHandler( explorer_FolderSwitch); } /// <summary>Handles the shutdown event of the add-in.</summary> /// <remarks>Unsubscribes from the folder switch event.</remarks> private void ThisAddIn_Shutdown(object sender, System.EventArgs e) { explorer.FolderSwitch += new Outlook.ExplorerEvents_10_FolderSwitchEventHandler( explorer_FolderSwitch); explorer = null; ribbonUI = null; } /// <summary>Handles the folder switch event of an explorer.</summary> void explorer_FolderSwitch() { // Set the Search string. if (ribbon.SearchText != null && ribbon.SearchText != string.Empty) { ribbon.SearchText = string.Empty; if (ribbonUI != null) { ribbonUI.Invalidate(); } } } Selecting an Outlook Item from the Explorer Selection When the user clicks Open Message with Highlighting, the user may have selected several items in the explorer. In the event handler for the button, the add-in iterates through the selected items and displays the first item that is either a MailItem object or a MeetingItem object, and then calls the The /// <summary>Handles the onAction event of the OpenMessage button. /// </summary> /// <param name="control">The source control of the event.</param> public void OpenMessage(Office.IRibbonControl control) { if (!string.IsNullOrWhiteSpace(currentSearchText)) { // Obtain a reference to the selection. Outlook.Selection selection = ActiveExplorer.Selection; if (selection.Count == 0) return; // Open the first item that is a supported Outlook item. // The selection may contain various item types. IEnumerator e = ActiveExplorer.Selection.GetEnumerator(); while (e.MoveNext()) { if (e.Current is Outlook.MailItem) { Outlook.MailItem item = e.Current as Outlook.MailItem; item.Display(); FindInInspector(item.GetInspector, currentSearchText); break; } else if (e.Current is Outlook.MeetingItem) { Outlook.MeetingItem item = e.Current as Outlook.MeetingItem; item.Display(); FindInInspector(item.GetInspector, currentSearchText); break; } } } } Highlighting a Search Term in an Open Item As shown in the following code, the add-in project contains a reference to the Microsoft.Office.Interop.Word assembly, and the SearchRibbon.cs file has a
using System; using System.Collections; using System.IO; using System.Reflection; using System.Runtime.InteropServices; using Office = Microsoft.Office.Core; using Outlook = Microsoft.Office.Interop.Outlook; using Word = Microsoft.Office.Interop.Word; namespace SearchHighlighting { [ComVisible(true)] public class SearchRibbon : Office.IRibbonExtensibility { The add-in defines a The
As shown in the following code, the /// <summary>Performs search highlighting in an open mail item. /// </summary> /// <param name="inspector">The inspector for the open item.</param> /// <param name="searchText">The text to highlight.</param> private void FindInInspector(Outlook.Inspector inspector, string term) { if (inspector == null || string.IsNullOrWhiteSpace(term)) return; if (inspector.IsWordMail()) { Word.Document doc = inspector.WordEditor as Word.Document; Word.Range wdRange = doc.Application.Selection.Range; Word.Find wdFind = wdRange.Find; wdFind.Format = false; wdFind.MatchCase = false; wdFind.MatchWholeWord = false; wdFind.HitHighlight(term, HighlightColor: Word.WdColor.wdColorYellow); } }
When a user performs an Instant Search within Outlook, Outlook highlights the found text in the Reading Pane. However, when the user opens an item from the explorer, the inspector does not highlight the text that you had searched on (as shown in Figure 2). Figure 2. Outlook item opened from the explorer
Fortunately, the add-in can use the Word object model to add highlighting to the Outlook item when it is opened. To keep this example simple, the add-in adds a Custom Search group to the Home tab for mail items, as shown in Figure 3. The Custom Search group includes a text box in which to enter a search term and an Open Message with Highlights button that opens the first applicable selected item in the current mail folder. Figure 3. Custom Search group on the ribbon UI
When the user opens an item with the Open Message with Highlights button, the add-in highlights all occurrences of the search term (as shown in Figure 4). Figure 4. Outlook item opened from the Custom Search UI
|
Watch the Video | Length: 5:59
|
Note:

