筛选定期约会并在主题中搜索某个字符串
该示例筛选“日历”文件夹中某一日期范围内的定期约会,然后以两种方式在主题中搜索字符串“office”。
示例
此代码示例使用 Items 集合(而不是 Table 对象)筛选定期约会,因为 Table 对象仅返回主要系列约会,不包括文件夹中的定期项。 为了在调用 Find(String) 或 Restrict(String) 方法时添加定期约会,此代码示例设置 Items 集合的 IncludeRecurrences 属性,然后按 Start 属性对文件夹中的约会进行排序。 然后,它使用 Jet 查询,指定定期约会的开始日期和结束日期。
获取指定日期范围内定期约会项的 Items 集合后,此代码示例使用 DAV 搜索和定位 (DASL) 查询执行其他两种搜索。 第一种搜索使用 Items.Find、FindNext 和 like 关键字,搜索主题中有“office”子字符串的项。 第二个搜索使用 Items.Restrict 方法和 ci_startswith 关键字 (keyword) 来搜索主题以“office”开头的项目。
如果使用 Visual Studio 测试此代码示例,必须先添加对 Microsoft Outlook 15.0 对象库组件的引用,并在导入 Microsoft.Office.Interop.Outlook 命名空间时指定 Outlook 变量。 不得将 Imports 或 using 语句直接添加到此代码示例中的函数前面,这两个语句必须后跟公共类声明。 下面的代码行演示了如何在 Visual Basic 和 C# 中执行导入和分配。
Imports Outlook = Microsoft.Office.Interop.Outlook
using Outlook = Microsoft.Office.Interop.Outlook;
Private Sub SearchRecurringAppointments()
Dim appt As Outlook.AppointmentItem = Nothing
Dim folder As Outlook.Folder = _
CType(Application.Session.GetDefaultFolder( _
Outlook.OlDefaultFolders.olFolderCalendar), _
Outlook.Folder)
' Set start value
Dim startTime As DateTime = New DateTime(2006, 8, 9, 0, 0, 0)
' Set end value
Dim endTime As DateTime = New DateTime(2006, 12, 14, 0, 0, 0)
' Initial restriction is Jet query for date range
Dim filter1 As String = "[Start] >= '" & startTime.ToString("g") _
& "' AND [End] <= '" & endTime.ToString("g") & "'"
Dim calendarItems As Outlook.Items = folder.Items.Restrict(filter1)
calendarItems.IncludeRecurrences = True
calendarItems.Sort("[Start]")
' Must use 'like' comparison for Find/FindNext
Dim filter2 As String
filter2 = "@SQL=" & _
Chr(34) & "urn:schemas:httpmail:subject" & Chr(34) & _
" like '%Office%'"
' Create DASL query for additional Restrict method
Dim filter3 As String
If (Application.Session.DefaultStore.IsInstantSearchEnabled) Then
filter3 = "@SQL=" & _
Chr(34) & "urn:schemas:httpmail:subject" & Chr(34) & " _
ci_startswith 'Office'"
Else
filter3 = "@SQL=" & _
Chr(34) & "urn:schemas:httpmail:subject" & Chr(34) & " _
like '%Office%'"
End If
' Use Find and FindNext methods
appt = CType(calendarItems.Find(filter2), Outlook.AppointmentItem)
While Not (appt Is Nothing)
Dim sb As StringBuilder = New StringBuilder()
sb.AppendLine(appt.Subject)
sb.AppendLine("Start: " & appt.Start)
sb.AppendLine("End: " & appt.End)
Debug.WriteLine(sb.ToString())
' Find the next appointment
appt = CType(calendarItems.FindNext(), Outlook.AppointmentItem)
End While
' Restrict calendarItems with DASL query
Dim restrictedItems As Outlook.Items = _
calendarItems.Restrict(filter3)
For Each apptItem As Outlook.AppointmentItem In restrictedItems
Dim sb As StringBuilder = New StringBuilder()
sb.AppendLine(apptItem.Subject)
sb.AppendLine("Start: " & apptItem.Start)
sb.AppendLine("End: " & apptItem.End)
sb.AppendLine()
Debug.WriteLine(sb.ToString())
Next
End Sub
private void SearchRecurringAppointments()
{
Outlook.AppointmentItem appt = null;
Outlook.Folder folder =
Application.Session.GetDefaultFolder(
Outlook.OlDefaultFolders.olFolderCalendar)
as Outlook.Folder;
// Set start value
DateTime start =
new DateTime(2006, 8, 9, 0, 0, 0);
// Set end value
DateTime end =
new DateTime(2006, 12, 14, 0, 0, 0);
// Initial restriction is Jet query for date range
string filter1 = "[Start] >= '" +
start.ToString("g")
+ "' AND [End] <= '" +
end.ToString("g") + "'";
Outlook.Items calendarItems = folder.Items.Restrict(filter1);
calendarItems.IncludeRecurrences = true;
calendarItems.Sort("[Start]", Type.Missing);
// Must use 'like' comparison for Find/FindNext
string filter2;
filter2 = "@SQL="
+ "\"" + "urn:schemas:httpmail:subject" + "\""
+ " like '%Office%'";
// Create DASL query for additional Restrict method
string filter3;
if (Application.Session.DefaultStore.IsInstantSearchEnabled)
{
filter3 = "@SQL="
+ "\"" + "urn:schemas:httpmail:subject" + "\""
+ " ci_startswith 'Office'";
}
else
{
filter3 = "@SQL="
+ "\"" + "urn:schemas:httpmail:subject" + "\""
+ " like '%Office%'";
}
// Use Find and FindNext methods
appt = calendarItems.Find(filter2)
as Outlook.AppointmentItem;
while (appt != null)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine(appt.Subject);
sb.AppendLine("Start: " + appt.Start);
sb.AppendLine("End: " + appt.End);
Debug.WriteLine(sb.ToString());
// Find the next appointment
appt = calendarItems.FindNext()
as Outlook.AppointmentItem;
}
// Restrict calendarItems with DASL query
Outlook.Items restrictedItems =
calendarItems.Restrict(filter3);
foreach (Outlook.AppointmentItem apptItem in restrictedItems)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine(apptItem.Subject);
sb.AppendLine("Start: " + apptItem.Start);
sb.AppendLine("End: " + apptItem.End);
sb.AppendLine();
Debug.WriteLine(sb.ToString());
}
}