Use the Table Object to Performantly Enumerate Filtered Items in a Folder

Office 2013 and later

Contribute to this content

Use GitHub to suggest and submit changes. See our guidelines for contributing to VBA documentation.

The code sample in this topic uses the Table object to enumerate a filtered set of items in the Inbox that were last modified after May 1, 2005. For each of these items, the code sample prints these values: the subject, the time that the item was last modified, and whether the item is hidden. The procedure is as follows:

  1. The sample defines a filter based on the value of the LastModificationTime property of mail items.

  2. It applies the filter to Folder.GetTable and obtains a Table of a subset of mail items in the Inbox that satisfies the filter.

    Note Note

    The returned table contains a default set of properties for each of the filtered items: EntryID, Subject, CreationTime, LastModificationTime, and MessageClass.

  3. It then uses Columns.RemoveAll and Columns.Add to update the Table with the actually desired properties: Subject, LastModificationTime, and the hidden attribute (PidTagAttributeHidden). It specifies properties with their explicit built-in names if they exist (for example, Subject, LastModificationTime), and only when they don't, it references the properties by their namespaces (for example, the hidden attribute of a mail item).

    Note Note

    The Table objects returned from Folder.GetTable in Step 2 and Columns.Add in Step 3 contain different property values but for the same set of filtered items in the Inbox.

  4. Lastly, it uses Table.GetNextRow to enumerate the filtered items (until Table.EndOfTable becomes true), displaying the values of the three desired properties for each item.

Sub DemoTable() 
 Dim Filter As String 
 Dim oRow As Outlook.Row 
 Dim oTable As Outlook.Table 
 Dim oFolder As Outlook.Folder 
 'Get a Folder object for the Inbox 
 Set oFolder = Application.Session.GetDefaultFolder(olFolderInbox) 
 'Define Filter to obtain items last modified after May 1, 2005 
 Filter = "[LastModificationTime] > '5/1/2005'" 
 'Restrict with Filter 
 Set oTable = oFolder.GetTable(Filter) 
 'Remove all columns in the default column set 
 'Specify desired properties 
 With oTable.Columns 
 .Add ("Subject") 
 .Add ("LastModificationTime") 
 'PidTagAttributeHidden referenced by the MAPI proptag namespace 
 .Add ("") 
 End With 
 'Enumerate the table using test for EndOfTable 
 Do Until (oTable.EndOfTable) 
 Set oRow = oTable.GetNextRow() 
 Debug.Print (oRow("Subject")) 
 Debug.Print (oRow("LastModificationTime")) 
 Debug.Print (oRow("")) 
End Sub
© 2015 Microsoft