0
votes

I have a VSTO application and I am trying to figure out how to get reference to a calendar recurring appointment and capture the last date in the series

I am searching first for items in a date range which i got from here, using:

Outlook.Folder calFolder =
    Application.Session.GetDefaultFolder(
    Outlook.OlDefaultFolders.olFolderCalendar)
    as Outlook.Folder;
DateTime start = DateTime.Now;
DateTime end = start.AddDays(5);
Outlook.Items rangeAppts = GetAppointmentsInRange(calFolder, start, end);
if (rangeAppts != null)
{
    foreach (Outlook.AppointmentItem appt in rangeAppts)
    {
        Debug.WriteLine("Subject: " + appt.Subject 
            + " Start: " + appt.Start.ToString("g"));
    }
}


private Outlook.Items GetAppointmentsInRange(
Outlook.Folder folder, DateTime startTime, DateTime endTime)
 {
string filter = "[Start] >= '"
    + startTime.ToString("g")
    + "' AND [End] <= '"
    + endTime.ToString("g") + "'";
Debug.WriteLine(filter);
try
{
    Outlook.Items calItems = folder.Items;
    calItems.IncludeRecurrences = true;
    calItems.Sort("[Start]", Type.Missing);
    Outlook.Items restrictItems = calItems.Restrict(filter);
    if (restrictItems.Count > 0)
    {
        return restrictItems;
    }
    else
    {
        return null;
    }
}
catch { return null; }

}

but I can't then figure out how to look into the recurring rules to calculate the last end date to an appointment. Is this possible in Outlook VSTO.

1

1 Answers

1
votes

Get a reference to a single AppointmentItem from your search results and then call GetRecurrencePattern to obtain a RecurrencePattern object. You can then evaluate .PatternEndDate to find the date of the last event in the series, and then use RecurrencePattern.GetOccurrence(date) to get a reference to that specific appointment.