创建使用 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 便会保存约会,再显示模式为“发生于 6 月的第一个星期一,有效期为 2007 年 6 月 1 日至 2016 年 6 月 6 日,时间范围为下午 2:00 到下午 5:00”的约会。

如果使用 Visual Studio 测试此代码示例,必须先添加对 Microsoft Outlook 15.0 对象库组件的引用,并在导入 Microsoft.Office.Interop.Outlook 命名空间时指定 Outlook 变量。 不得将 using 语句直接添加到此代码示例中的函数前面,这个语句必须后跟公共类声明。 下面的代码行演示了如何在 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);
}

另请参阅