共用方式為


搜尋並取得某個時間範圍內的約會

此範例會傳回預設 Microsoft Outlook 行事曆中特定時間範圍內的約會。

範例

此程式代碼範例包含兩種方法:DemoAppointmentsInRange 和 GetAppointmentsInRange。 DemoAppointmentsInRange 會取得目前已登入 Outlook 配置檔的預設行事歷、設定從今天上午 12:00 開始的 5 天日期範圍、呼叫 GetAppointmentsInRange 以取得落在該時間範圍內的約會,並顯示每個傳回約會的主旨和開始時間。

GetAppointmentsInRange 接受 Outlook 資料夾,以及時間範圍的開始和結束 DateTime 值作為輸入參數。 這個方法會使用 Restrict (String) 方法,以及 Jet 格式的字串篩選條件,以傳回在指定時間範圍內開始和結束的約會。 假設 [Start] 和 [End] 是約會的開始時間和結束時間,且 startTime 和 endTime 是指定時間範圍的開始和結束時間,GetAppointmentsInRange 會設定篩選條件,以尋找具有 [Start]>=startTime、 和 [End]<=endTime的約會。 下列程式代碼顯示 C# 中的 Jet 篩選。

string filter = "[Start] >= '"
    + startTime.ToString("g")
    + "' AND [End] <= '"
    + endTime.ToString("g") + "'";

在呼叫 Items.Restrict 方法來搜尋約會之前,GetAppointmentsInRange 會執行其他兩件事,以包含在指定時間範圍內發生的週期性約會:

或者,如果您對部分或完全與指定時間範圍重疊的約會感興趣,您會指定不同的篩選條件來傳回其他類型的約會 (如圖 1) 所示:

  • 在指定時間範圍內開始和結束的約會 (例如約會 A) :

    [Start]>=startTime and [End]<=endTime

  • 在指定的時間範圍之前開始,但在時間範圍內結束的約會 (例如約會 B) :

    [Start]<startTime and [End]<=endTime

  • 在指定的時間範圍內開始,但在時間範圍之後結束的約會 (例如,約會 C) :

    [Start]>=startTime and [End]>endTime

  • 在指定的時間範圍之前開始且在時間範圍之後結束的約會 (例如,約會 D) :

    [Start]<startTime and [End]>endTime

圖 1: 在某個時間範圍內發生的約會類型,或與該時間範圍重疊的約會類型

在某個時間範圍內發生的約會類型,或與該時間範圍重疊的約會類型

因為在任何時間範圍內 startTime<=endTime,具有 [Start]<=endTime[End]>=startTime 的篩選條件都會擷取該時間範圍內的上述約會類型。

在 C# 中,您可以如下所示表示 Jet 篩選。

string filter = "[Start] <= '"
    + endTime.ToString("g")
    + "' AND [End] >= '"
    + startTime.ToString("g") + "'";

下列程式代碼顯示完整的範例。 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 void DemoAppointmentsInRange()
{
    Outlook.Folder calFolder =
        Application.Session.GetDefaultFolder(
        Outlook.OlDefaultFolders.olFolderCalendar)
        as Outlook.Folder;
    DateTime start = DateTime.Now;
    DateTime end = start.AddDays(5);
    Outlook.Items rangeAppts = GetAppointmentsInRange(calFolder, start, end);
    if (rangeAppts != null)
    {
        foreach (Outlook.AppointmentItem appt in rangeAppts)
        {
            Debug.WriteLine("Subject: " + appt.Subject 
                + " Start: " + appt.Start.ToString("g"));
        }
    }
}

/// <summary>
/// Get recurring appointments in date range.
/// </summary>
/// <param name="folder"></param>
/// <param name="startTime"></param>
/// <param name="endTime"></param>
/// <returns>Outlook.Items</returns>
private Outlook.Items GetAppointmentsInRange(
    Outlook.Folder folder, DateTime startTime, DateTime endTime)
{
    string filter = "[Start] >= '"
        + startTime.ToString("g")
        + "' AND [End] <= '"
        + endTime.ToString("g") + "'";
    Debug.WriteLine(filter);
    try
    {
        Outlook.Items calItems = folder.Items;
        calItems.IncludeRecurrences = true;
        calItems.Sort("[Start]", Type.Missing);
        Outlook.Items restrictItems = calItems.Restrict(filter);
        if (restrictItems.Count > 0)
        {
            return restrictItems;
        }
        else
        {
            return null;
        }
    }
    catch { return null; }
}

另請參閱