共用方式為


建立使用 YearNth 模式的年度週期性約會

此範例示範如何建立年度週期模式為特定日期的約會,例如 6 月的第一個星期一。

範例

注意事項

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

如果您想要建立一個年度約會,在特定月份 (例如,6 月的第一個星期一) ,您必須使用 YearNth 週期。 若要設定 YearNth 週期,您必須先將 RecurrencePattern 物件的 RecurrenceType 屬性設定為 olRecursYearNth。 然後將 DayOfWeekMask 屬性設定為指定約會應該在一周中的哪一天重複,並設定 Instance 屬性來指定指定的星期幾第 N 次出現 (例如,指定月份中的第三個星期二) 每年模式。

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

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

在下列程式代碼範例中,RecurringYearNthAppointment 會建立具有 YearNth 週期模式的約會。 RecurringYearNthAppointment 會先建立 AppointmentItem 物件來建立週期性約會。 接下來,它會使用 GetRecurrencePattern () 方法來取得約會的週期模式。 然後設定下列 RecurrencePattern 屬性:RecurrenceType、DayOfWeekMask、 MonthOfYearInstanceOccurrencesDurationPatternStartDateStartTimeEndTime。 MonthOfYear 屬性可以採用 1 到 12 的數值,其中每個數位都代表對應的月份。 設定屬性之後,RecurringYearNthAppointment 會儲存約會,然後顯示其模式「發生於 2007 年 6 月 6 日的第一個星期一,直到 2016 年 6 月 6 日下午 2:00 到下午 5:00」。

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 RecurringYearNthAppointment()
{
    Outlook.AppointmentItem appt = Application.CreateItem(
        Outlook.OlItemType.olAppointmentItem)
        as Outlook.AppointmentItem;
    appt.Subject = "Recurring YearNth Appointment";
    Outlook.RecurrencePattern pattern = appt.GetRecurrencePattern();
    pattern.RecurrenceType = Outlook.OlRecurrenceType.olRecursYearNth;
    pattern.DayOfWeekMask = Outlook.OlDaysOfWeek.olMonday;
    pattern.MonthOfYear = 6;
    pattern.Instance = 1;
    pattern.Occurrences = 10;
    pattern.Duration = 180;
    pattern.PatternStartDate = DateTime.Parse("6/1/2007");
    pattern.StartTime = DateTime.Parse("2:00:00 PM");
    pattern.EndTime = DateTime.Parse("5:00:00 PM");
    appt.Save();
    appt.Display(false);
}

另請參閱