How to: Display Selected Items in the Active Explorer

This example shows how to use the OutlookItem helper class to conveniently display all the items selected in the active Explorer window.

Example

Programming Applications for Office Outlook 2007

The following code example is an excerpt from Programming Applications for Microsoft Office Outlook 2007, from Microsoft Press (ISBN 9780735622494, copyright Microsoft Press 2007, all rights reserved).

Buy this book

Sample chapters

The Selection object contains the set of Outlook items currently selected in the active Outlook explorer. Neither the active explorer, represented by ActiveExplorer(), nor the set of selected items indicates the type of the items that are selected. Normally, you would have to first identify the item type and then call the specific Display method for that type. Because the Display method is common to all Outlook items objects and the OutlookItem helper class includes this method, you can take advantage of the helper class, by declaring an instance of the OutlookItem object, myItem, and using myItem.Display to display each item in the selection. You can see the implementation of the OutlookItem helper class in How to: Create a Helper Class to Access Common Outlook Item Members

If you use Microsoft Visual Studio to test this code example, you must first add a reference to the Microsoft Outlook 14.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 DisplaySelectedItems()
{
    Outlook.Selection selection =
        Application.ActiveExplorer().Selection;
    for (int i = 1; i <= selection.Count; i++)
    {
        OutlookItem myItem = new OutlookItem(selection[i]);
        myItem.Display();
    }
}

See Also

Tasks

How to: Create a Helper Class to Access Common Outlook Item Members

Other Resources

General Outlook Items