kjob


I am trying to create code behind a custom form in Outlook. It is a customization for the AppointmentItem form. On the custom tab we have a C# User Control. Within the control, I have a need to get all of the occurrences of the appointment specified by the recurrence pattern, if one exists.

So, based on some user action on the control, I am trying to get a list of dates that the appointment will fall on. The problem is, when I call GetOccurrence on the recurrence pattern of the item, I get an exception
"You changed one of the recurrences of this item, and this instance no longer exists. Close any open items and try again."

What am I missing here Any help would be appreciated.

Example code:

private void button1_Click(object sender, EventArgs e)
{
List<DateTime> lsStarts = new List<DateTime>();
List<DateTime> lsEnds = new List<DateTime>();

Outlook.Application app = new Outlook.Application();
Outlook.Inspector insp = app.ActiveInspector();
Object item = insp.CurrentItem;
Outlook.AppointmentItem apptItem = item as Outlook.AppointmentItem;

LoadDateLists(apptItem, lsStarts, lsEnds);

// continue, i.e. check availability of selected resource for the dates of the appointment
//ShowDates(lsStarts, lsEnds);
}

private void LoadDateLists(Outlook.AppointmentItem item, List<DateTime> lsStarts, List<DateTime> lsEnds)
{
lsStarts.Clear();
lsEnds.Clear();
if (item != null)
{
if (item.RecurrenceState == Microsoft.Office.Interop.Outlook.OlRecurrenceState.olApptNotRecurring)
{
lsStarts.Add(TimeZone.CurrentTimeZone.ToUniversalTime(item.Start));
lsEnds.Add(TimeZone.CurrentTimeZone.ToUniversalTime(item.End));
}
else if (item.RecurrenceState == Microsoft.Office.Interop.Outlook.OlRecurrenceState.olApptMaster)
{
Outlook.RecurrencePattern recurPattern = item.GetRecurrencePattern();
for (DateTime dt = recurPattern.PatternStartDate; dt < recurPattern.PatternEndDate; dt = dt.AddDays(1))
{
try
{
// THE FOLLOWING LINE CAUSES EXCEPTION:
// You changed one of the recurrences of this item, and this instance no longer exists.
// Close any open items and try again.
Outlook.AppointmentItem oItem = recurPattern.GetOccurrence(dt);
if (oItem != null)
{
lsStarts.Add(TimeZone.CurrentTimeZone.ToUniversalTime(oItem.Start));
lsEnds.Add(TimeZone.CurrentTimeZone.ToUniversalTime(oItem.End));
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
else
{
// to do: Unhandled condition in LoadDateLists, item.RecurrenceState == " + item.RecurrenceState.ToString()
}
}
}





Re: Visual Studio Tools for Office Before an Appointment item is saved, how can I get the occurrences specified by the recurrence pattern?

Sue Mosher - Outlook MVP

If the user has changed the date/time of an occurrence or deleted it, GetOccurrence will not return an instance for that original date/time. You'd need to handle that error possibility and look for that original date/time to be represented in the RecurrencePattern.Exceptions collection.





Re: Visual Studio Tools for Office Before an Appointment item is saved, how can I get the occurrences specified by the recurrence pattern?

kjob

The RecurrencePattern.Exceptions collection is empty. This code is acting within a custom form - in this case on a brand new AppointmentItem object. The idea is that before the item is saved, the user would be able verify the availability of some resources (i.e. video conference equipment) by using the controls on the the extra tab we are putting on the custom form.

At the point this code is called, the user has just set the recurrence pattern and then maybe selected a resource - I am trying to use the recurrence pattern to identify the date/times of the appointment.

An exception with the message "You changed one of the recurrences of this item, and this instance no longer exists. Close any open items and try again." is being thrown for each date I pass to the GetOccurrence method, as I loop from the PatternStartDate to the PatternEndDate of the RecurrencePattern object.

Any ideas





Re: Visual Studio Tools for Office Before an Appointment item is saved, how can I get the occurrences specified by the recurrence pattern?

Sue Mosher - Outlook MVP

Here's one idea: Your dt variable's value is built from just the date, not the date plus the time.





Re: Visual Studio Tools for Office Before an Appointment item is saved, how can I get the occurrences specified by the recurrence pattern?

kjob

The dt variable is of DateTime type - it does have a time part in it, although it was being set to 12:00:00 am. I tried adding a second to the variable before calling GetOccurrence, but the result was the same: Instead of returning nothing or the occurrence for that date, the same exception is thrown.

What could possibly be going on with this item that would be causing this





Re: Visual Studio Tools for Office Before an Appointment item is saved, how can I get the occurrences specified by the recurrence pattern?

kjob

Ok, I got it working with Sue's hint - adding the time part to the date. The documentation for the GetOccurrence method

could use some clarification. I modified the loop code as follows and now it does get me the expected occurrences:

for (DateTime dt = recurPattern.PatternStartDate; dt <= recurPattern.PatternEndDate; dt = dt.AddDays(1))

{

DateTime dt2 = dt;

try

{

dt2 = dt2.AddHours(item.Start.Hour);

dt2 = dt2.AddMinutes(item.Start.Minute);

dt2 = dt2.AddSeconds(item.Start.Second);

Outlook.AppointmentItem oItem = recurPattern.GetOccurrence(dt2);

if (oItem != null)

{

lsStarts.Add(TimeZone.CurrentTimeZone.ToUniversalTime(oItem.Start));

lsEnds.Add(TimeZone.CurrentTimeZone.ToUniversalTime(oItem.End));

}

}

catch (Exception ex)

{

// exceptions expected, don't do anything here

//MessageBox.Show(ex.Message);

}

}

Thanks Sue for the hint.