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;
outl开发者_StackOverflowookCalendarItems.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?
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);
}
}
}
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
精彩评论