共用方式為


在週期性約會系列中建立例外狀況約會

此範例會使用 Exception 物件來建立約會標準週期模式的例外狀況。

範例

注意事項

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

一旦您刪除或變更週期性約會的一個約會實例,Outlook 就會建立 Exception 物件。 Exception 物件可讓您建立標準週期模式的例外狀況。 對象的屬性包含對約會實例所做的變更。 Exceptions 集合包含週期性約會的所有 Exception 物件,並與約會的 RecurrencePattern 對象相關聯。

若要取得代表週期性約會原始週期模式例外狀況的 AppointmentItem 物件,請使用 Exception 的 AppointmentItem 屬性。 藉由使用傳回 AppointmentItem 的方法和屬性,您可以設定約會例外狀況的屬性。

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

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

在下列程式代碼範例中,CreateExceptionExample 會變更在週期性約會 系列中尋找特定約會主題中建立的週期性約會主旨,然後使用所產生 Exception 物件的 AppointmentItem 屬性來擷取對應至約會例外狀況的 AppointmentItem。 CreateExceptionExample 接著會變更約會例外狀況的開始和結束時間。

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 CreateExceptionExample()
{
    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 myInstance =
                pattern.GetOccurrence(DateTime.Parse(
                "7/21/2006 2:00 PM"))
                as Outlook.AppointmentItem;
            if (myInstance != null)
            {
                myInstance.Subject = "My Exception";
                myInstance.Save();
                Outlook.RecurrencePattern newPattern =
                    appt.GetRecurrencePattern();
                Outlook.Exception myException =
                    newPattern.Exceptions[1];
                if (myException != null)
                {
                    Outlook.AppointmentItem myNewInstance =
                        myException.AppointmentItem;
                    myNewInstance.Start =
                        DateTime.Parse("7/21/2006 1:00 PM");
                    myNewInstance.End =
                        DateTime.Parse("7/21/2006 2:00 PM");
                    myNewInstance.Save();
                }
            }
        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex.Message);
        }
    }
}

另請參閱