Filter and efficiently enumerate items in a folder
This example shows how to use the Table object to filter for items in the Inbox that have attachments, and efficiently enumerate such items, displaying selected properties for each item.
Example
The code sample specifies the property PR_HASATTACH with the MAPI namespace, and uses the property to create the initial filter in the GetTable method on the Inbox. A Table object for an item type has default columns representing certain properties of that item type. To customize the columns, this example first calls the RemoveAll method on the Columns collection of that Table, and then calls the Add method on the Columns collection to add the EntryID, Subject, and ReceivedTime properties using the built-in property names, with the ReceivedTime column storing values in local date-time representation.
The example then calls Columns.Add one more time to add the ReceiveTime property specifying its MAPI namespace so that this column will store the value as a Universal Coordinated Time (UTC) date-time value. Finally, the example enumerates each item in the Table, displaying the value of each of the four properties specified as the table columns.
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 Imports or using statement must not occur directly before the functions in the code example but must be added before the public Class declaration. The following lines of code show how to do the import and assignment in Visual Basic and C#.
Imports Outlook = Microsoft.Office.Interop.Outlook
using Outlook = Microsoft.Office.Interop.Outlook;
Private Sub DemoTableColumns()
Const PR_HAS_ATTACH As String = _
"http://schemas.microsoft.com/mapi/proptag/0x0E1B000B"
' Obtain Inbox
Dim folder As Outlook.Folder = _
CType(Application.Session.GetDefaultFolder( _
Outlook.OlDefaultFolders.olFolderInbox), _
Outlook.Folder)
' Create filter
Dim filter As String = "@SQL=" & Chr(34) _
& PR_HAS_ATTACH & Chr(34) & " = 1"
Dim table As Outlook.Table = _
folder.GetTable(filter, _
Outlook.OlTableContents.olUserItems)
' Remove default columns
table.Columns.RemoveAll()
' Add using built-in name
table.Columns.Add("EntryID")
table.Columns.Add("Subject")
table.Columns.Add("ReceivedTime")
table.Sort("ReceivedTime", Outlook.OlSortOrder.olDescending)
' Add using namespace
' Date received
table.Columns.Add( _
"urn:schemas:httpmail:datereceived")
While Not (table.EndOfTable)
Dim nextRow As Outlook.Row = table.GetNextRow()
Dim sb As StringBuilder = New StringBuilder()
sb.AppendLine(nextRow("Subject").ToString())
' Reference column by name
sb.AppendLine("Received (Local): " _
& nextRow("ReceivedTime").ToString())
' Reference column by index
sb.AppendLine("Received (UTC): " & nextRow(4).ToString())
sb.AppendLine()
Debug.WriteLine(sb.ToString())
End While
End Sub
private void DemoTableColumns()
{
const string PR_HAS_ATTACH =
"http://schemas.microsoft.com/mapi/proptag/0x0E1B000B";
// Obtain Inbox
Outlook.Folder folder =
Application.Session.GetDefaultFolder(
Outlook.OlDefaultFolders.olFolderInbox)
as Outlook.Folder;
// Create filter
string filter = "@SQL=" + "\""
+ PR_HAS_ATTACH + "\"" + " = 1";
Outlook.Table table =
folder.GetTable(filter,
Outlook.OlTableContents.olUserItems);
// Remove default columns
table.Columns.RemoveAll();
// Add using built-in name
table.Columns.Add("EntryID");
table.Columns.Add("Subject");
table.Columns.Add("ReceivedTime");
table.Sort("ReceivedTime", Outlook.OlSortOrder.olDescending);
// Add using namespace
// Date received
table.Columns.Add(
"urn:schemas:httpmail:datereceived");
while (!table.EndOfTable)
{
Outlook.Row nextRow = table.GetNextRow();
StringBuilder sb = new StringBuilder();
sb.AppendLine(nextRow["Subject"].ToString());
// Reference column by name
sb.AppendLine("Received (Local): "
+ nextRow["ReceivedTime"]);
// Reference column by index
sb.AppendLine("Received (UTC): " + nextRow[4]);
sb.AppendLine();
Debug.WriteLine(sb.ToString());
}
}