Filter and display Inbox items modified in the last month
This example shows how to filter and display Inbox items that were modified in the last month.
Example
Note
The following code example is an excerpt from Programming Applications for Microsoft Office Outlook 2007.
DAV Searching and Locating (DASL) query language is based on the Microsoft Exchange implementation of DASL in Outlook. It can be used to return property-based results for item-level searches in folder data, such as that represented by a Table object. DASL filters support string comparisons, including equivalence, prefix, phrase, and substring matching, by using the equal (=) operator. You can use DASL queries to perform date-time comparison and filtering.
Because DASL queries always perform DateTime comparisons in Coordinated Universal Time (UTC), you must convert the local time value to UTC if you want the query to operate correctly. You must also convert the DateTime value to a string representation because DASL filters support string comparisons. You can make the DateTime conversion in two ways: by using the LocalTimeToUTC(Object) method of the Row object, or by using Outlook DateTime macros to make the conversion.
The following line of code shows how to use the LocalTimeToUTC method to convert the value of the LastModificationTime property (which is a default column in all Item objects) to UTC.
DateTime modified = nextRow.LocalTimeUTC(“LastModificationTime”);
The following table lists the DateTime macros you can use to return filtered strings that compare the value of a given DateTime property with a specified relative date or date range in UTC. The SchemaName property value represents any valid DateTime property referenced by namespace.
Macro |
Syntax |
Description |
---|---|---|
today |
%today(“SchemaName”)% |
Restricts for items with a SchemaName property value equal to today. |
tomorrow |
%tomorrow(“SchemaName”)% |
Restricts for items with a SchemaName property value equal to tomorrow. |
yesterday |
%yesterday(“SchemaName”)% |
Restricts for items with a SchemaName property value equal to yesterday. |
next7days |
%next7days(“SchemaName”)% |
Restricts for items with SchemaName property values in a range equivalent to the next seven days. |
last7days |
%last7days(“SchemaName”)% |
Restricts for items with SchemaName property values in a range equivalent to the last seven days. |
nextweek |
%nextweek(“SchemaName”)% |
Restricts for items with SchemaName property values in a range equivalent to next week. |
thisweek |
%thisweek(“SchemaName”)% |
Restricts for items with SchemaName property values in a range equivalent to this week. |
lastweek |
%lastweek(“SchemaName”)% |
Restricts for items with SchemaName property values in a range equivalent to last week. |
nextmonth |
%nextmonth(“SchemaName”)% |
Restricts for items with SchemaName property values in a range equivalent to next month. |
thismonth |
%thismonth(“SchemaName”)% |
Restricts for items with SchemaName property values in a range equivalent to this month. |
lastmonth |
%lastmonth(“SchemaName”)% |
Restricts for items with SchemaName property values in a range equivalent to last month. |
In the following example, DemoDASLDateMacro creates a DASL query that uses the lastmonthDateTime macro to filter for items in the user’s Inbox that were modified in the last month. It then creates a Table object with that filter, and enumerates and displays the rows in the restricted Table object.
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 DemoDASLDateMacro()
{
string filter = "@SQL=" + "%lastmonth(" + "\"" +
"DAV:getlastmodified" + "\"" + ")%";
Outlook.Table table = Application.Session.GetDefaultFolder(
Outlook.OlDefaultFolders.olFolderInbox).GetTable(
filter, Outlook.OlTableContents.olUserItems);
while (!table.EndOfTable)
{
Outlook.Row row = table.GetNextRow();
Debug.WriteLine(row["Subject"]);
}
}