How to: Use Arrays to Efficiently Enumerate Items in a Folder

Office 2013 and later

This example shows how to efficiently enumerate items in a Folder object by using the GetArray(Int32) method.

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

In the following code example, DemoGetArrayForTable gets a Table object from a Folder object by using the GetTable(Object, Object) method. DemoGetArrayForTable then uses the GetArray method to return an Array object that contains elements for every row in the table. The returned Array object is a two-dimensional array that represents a set of row and column values from the Table. The array is zero-based, instead of one-based as is the case with Outlook collections. Once the Array object is obtained, the code uses a for loop to enumerate through the table.

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 DemoGetArrayForTable()
    // Obtain Inbox
    Outlook.Folder folder =
        as Outlook.Folder;
    Outlook.Table table =
        folder.GetTable("", Outlook.OlTableContents.olUserItems);
    Array tableArray = table.GetArray(table.GetRowCount()) as Array;
    for (int i = 0; i <= tableArray.GetUpperBound(0); i++)
        for (int j = 0; j <= tableArray.GetUpperBound(1); j++)
            Debug.WriteLine(tableArray.GetValue(i, j));

Other resources