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.

Example

Note

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

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 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 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"]);
    }
}

See also