0
votes

I want to catch the change event on an AppointmentItem. I use Outlook 2017 for tests. To achieve I use:

I attached the events like this:

 public void AttachEvents()
        {
            _CalendarItems.ItemAdd += Item_Add;
            _CalendarItems.ItemChange += Item_Change;
            _DeletedItems.ItemAdd += Item_Delete_Add;

The Item_Change method looks like this:

 public void Item_Change(Object item)
        {
            if (item != null && item is Outlook.AppointmentItem)
            {
                Outlook.AppointmentItem myAppointment = item as Outlook.AppointmentItem;

To test the code I created a recurring appointment series. I double-clicked on appointment in the calendar and entered some title and body and saved. Now I started my code and inspected the item. Unfortunately, item points to the series and NOT to the individual appointment when item changed is initiated. How can I retrieve the actual AppointmentItem when Item_Changed is initiated?

Related Stackoverflow Posting: Outlook Addin: Moving Appointment in Calendar does not reflect new date/time in AppointmentItem (catch Calendar.ItemChange) But still there is no solution to this

More on this topic:

2

2 Answers

0
votes

Exceptions are not actual appointments - they are stored as embedded message attachments on the master appointments. You get the master appointment, and you would need to access its exceptions to see what changed.

0
votes

There is a tricky solution to that. Assumption: The user uses the calendar view to change the item. As the event is thrown by the calendar view this should be true in all cases:

_CalendarItems = calendarFolder.Items;
_CalendarItems.ItemChange += Item_Change;

[...] now we can use the CalendarView to calculate the selected Startdate and compare it to all Exceptions stored in the RecurrencePattern ...

if (myAppointment.IsRecurring)
                        {
                            // in case of recurring appointments at this point we always get
                            // only a reference to the series master NOT the occurrence                            
                            // Assumption: The user clicked on the AppointmentItem in the calendar view
                            // So we can calculate the selected Start Time from this selection range
                            // then compare this against all Exceptions in the OccurrencePattern of the recurring pattern
                            // if we find one AppointmentItem in the Exceptions which has the same DateTime then we found the correct one.
                            //
                            Outlook.Application application = new Outlook.Application();
                            Outlook.Explorer explorer = application.ActiveExplorer();
                            Outlook.Folder folder = explorer.CurrentFolder as Outlook.Folder;
                            Outlook.View view = explorer.CurrentView as Outlook.View;


                            // get the current calendar view
                            if (view.ViewType == Outlook.OlViewType.olCalendarView)
                            {
                                Outlook.CalendarView calView = view as Outlook.CalendarView;
                                Outlook.RecurrencePattern pattern = myAppointment.GetRecurrencePattern();

                                for (int i = 1; i <= pattern.Exceptions.Count; i++)
                                {
                                    Outlook.Exception myException = pattern.Exceptions[i];
                                    Outlook.AppointmentItem exceptionItem = myException.AppointmentItem;
                                    DateTime itemDateStart = exceptionItem.Start;
                                    if (itemDateStart == calView.SelectedStartTime)
                                    {
                                        updateMyPluginMeeting(exceptionItem);
                                        return; // the use may only select on AppointmentItem so we can skip the rest
                                    }
                                }
                            }

                        }

If you know any better solution to this let me know.