Use instant search to search all folders and all stores for a phrase in the subject

This example uses Instant Search to search all folders and all stores for a phrase in the subject, and then displays the items in an explorer window.

Example

Note

The following code example is an excerpt from Programming Applications for Microsoft Office Outlook 2007.

Instant Search is a feature of Microsoft Outlook that enables you to search by issuing queries that return results based on the content. Once your query has been processed, the results can be returned in a variety of objects, including the Table object, the Items collection, and the Search object. You can write code that uses Instant Search by using the Advanced Query Syntax (AQS) that is offered by Microsoft Windows Desktop Search. AQS is one of three query languages that Outlook supports. It is powerful, but limited to the Search(String, OlSearchScope) method of the Explorer object. You cannot use AQS to provide a restriction for Table or Item objects. In addition, the results returned by an AQS query can be displayed only in the Outlook user interface. The following table lists the three query languages that Outlook supports; however, this topic will illustrate the use of only AQS.

Query language

Description

AQS

AQS is used by Windows Desktop Search and is the query language for the Instant Search feature in Outlook.

DASL

DAV Search and Locating (DASL) query language is based on the Microsoft Exchange implementation of DASL in Outlook. DASL can be used to return results in the Table object.

Jet

Jet query language provides a simple query language for Outlook, and is based on the Microsoft Jet Expression Service. Jet is used to create filter strings for the Restrict methods of the Items collection and the Table object.

In the following code example, DemoInstantSearch gets all mail folders in all stores where indexing is enabled by using the IsInstantSearchEnabled property of the Store object. It then uses the Search method of the Explorer object to filter for all items that contain the exact phrase “Office 2007” in the subject and that have been received in the last month. The results of the search are finally displayed in a separate explorer window.

If you use Visual Studio to test this code example, you must first add a reference to the Microsoft Outlook 15.0 Object Library component and specify the Outlook variable when you import the Microsoft.Office.Interop.Outlook namespace. The using statement must not occur directly before the functions in the code example but must be added before the public Class declaration. The following line of code shows how to do the import and assignment in C#.

using Outlook = Microsoft.Office.Interop.Outlook;
private void DemoInstantSearch()
{
    if (Application.Session.DefaultStore.IsInstantSearchEnabled)
    {
        Outlook.Explorer explorer = Application.Explorers.Add(
            Application.Session.GetDefaultFolder(
            Outlook.OlDefaultFolders.olFolderInbox)
            as Outlook.Folder,
            Outlook.OlFolderDisplayMode.olFolderDisplayNormal);
        string filter = "subject:" +
            "\"" + "Office 2007" + "\"" +
            " received:(last month)";
        explorer.Search(filter,
            Outlook.OlSearchScope.olSearchScopeAllFolders);
        explorer.Display();
    }
}

See also