共用方式為


在週期性約會系列中尋找特定約會

此範例示範如何傳回 AppointmentItem 物件,此物件代表週期性約會系列中的特定約會。

範例

注意事項

下列程式代碼範例是 Microsoft Office Outlook 2007 程式設計應用程式的摘錄。

若要尋找在指定日期和時間發生的週期性約會實例,請使用 RecurrencePattern 物件的 GetOccurrence (DateTime) 方法來傳回 AppointmentItem 物件。

當您使用週期性約會專案時,您應該釋放任何先前的參考、在存取或修改專案之前取得週期性約會專案的新參考,並在完成並儲存變更後立即釋放這些參考。 此作法適用於週期性 AppointmentItem 物件,以及任何 ExceptionRecurrencePattern 物件。 若要在 Visual Basic 中釋放參考,請將該現有物件設定為 Nothing。 在 C# 中,明確釋放該物件的記憶體。

請注意,即使在您釋放您的參考並嘗試取得新的參考之後,如果另一個載入宏或 Outlook) 另一個載入宏 (保留使用中參考,您的新參照仍會指向物件的過期複本。 因此,請務必在完成週期性約會後立即釋出您的參考。

在下列程式代碼範例中,CheckOccurrenceExample 會使用在建立具有 每周模式的週期性約會中的程式代碼範例中建立的週期性約會。 然後,它會呼叫 GetOccurrence 方法來判斷週期性約會是否在指定的日期和時間開始。 為了確保即使提供的資訊不符合週期性約會實例的開始日期和時間,程式仍會繼續,此範例會使用 try...catch 區塊。 在週期性約會系列的每個約會上呼叫 GetOccurrence 方法之後,CheckOccurrenceExample 會測試 singleAppt 變數,以判斷它是否設定為 Null 參考,指出方法失敗且未傳回 AppointmentItem 物件。

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 CheckOccurrenceExample()
{
    Outlook.AppointmentItem appt = Application.Session.
        GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar).
        Items.Find(
        "[Subject]='Recurring Appointment DaysOfWeekMask Example'")
        as Outlook.AppointmentItem;
    if (appt != null)
    {
        try
        {
            Outlook.RecurrencePattern pattern =
                appt.GetRecurrencePattern();
            Outlook.AppointmentItem singleAppt =
                pattern.GetOccurrence(DateTime.Parse(
                "7/21/2006 2:00 PM"))
                as Outlook.AppointmentItem;
            if (singleAppt != null)
            {
                Debug.WriteLine("7/21/2006 2:00 PM occurrence found.");
            }
        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex.Message);
        }
    }
}

另請參閱