Export (0) Print
Expand All

How to: Enumerate Items in the Inbox Based on the Last Modification Time

This example shows how to enumerate items in the Inbox folder based on the last modification time.

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 Table object represents a set of items from a Folder or Search object. To obtain a Table, call the GetTable(Object, Object) method on a Folder or Search object. Each item in the returned Table contains only a default subset of the item’s properties. Each Row object can be regarded as an item in the folder, and each Column object as a property of an item. Removing, adding, or changing rows is not supported in the Table. To enumerate items in a Table, first use the EndOfTable property to see whether your current position is at the end of the table. If EndOfTable returns false, use the GetNextRow() method to return a Row, which contains a default number of Column objects. You continue iterating in a forward manner through the Table by calling GetNextRow until EndOfTable returns true.

In the following code example, DemoTableForInbox obtains a Table object for the Inbox folder, sorts the Table object by using the LastModificationTime property and Sort(String, Object) method, and iterates through the table to write the subject of each item to the trace listeners of the Listeners collection.

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 DemoTableForInbox()
{
    //Obtain Inbox
    Outlook.Folder folder =
        Application.Session.GetDefaultFolder(
        Outlook.OlDefaultFolders.olFolderInbox)
        as Outlook.Folder;
    //Obtain Table using defaults
    Outlook.Table table =
        folder.GetTable(Type.Missing, Type.Missing);
    table.Sort("LastModificationTime",
        Outlook.OlSortOrder.olDescending);
    while (!table.EndOfTable)
    {
        Outlook.Row nextRow = table.GetNextRow();
        Debug.WriteLine(nextRow["Subject"]);
    }
}

Other Resources

Community Additions

ADD
Show:
© 2014 Microsoft