问题
I was trying to list all appointments in the default folder, like so:
Outlook.MAPIFolder calendarFolder = outlookApp.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);
Outlook.Items outlookCalendarItems = calendarFolder.Items;
outlookCalendarItems.IncludeRecurrences = true;
List<Outlook.AppointmentItem> lst = new List<Outlook.AppointmentItem>();
foreach (Outlook.AppointmentItem item in outlookCalendarItems)
{
lst.Add(item);
}
This lists all the appointments, except the recurring appointments - it only lists the first ocurrence. Is there a way to add all recurrences to this list?
回答1:
Try using the AppointmentItem.GetRecurrancePattern method (and RecurrencePattern type) off the appointment item, then you can iterate over them.
An example of getting a single occurrence can be found here:
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);
}
}
}
回答2:
If you sort by start time, you can then use the IncludeRecurrences property to get all appointment items. One word of caution - if you have recurring appointments that don't have an end, you'll get an infinite loop - so be sure and test for an end date.
See: https://msdn.microsoft.com/en-us/library/bb220097(v=office.12).aspx
来源:https://stackoverflow.com/questions/6425776/outlook-2010-how-to-get-a-list-of-all-appointments-including-recurrences