Outlook 2007 add-in: appointment changed event

J

Johan Machielse

Dear reader,

I've created an add-in for Outlook 2007; this add-in sets the color of the
appointments related to the location.

- In the OnStartupComplete() event all appointments will be colored.
- In the Outlook.MAPIFolder.AddItem event new appointmets will be colored.

But, it goes wrong with the Outlook.MAPIFolder.ChangeItem event...

//_calendarItems is of type Outlook.MAPIFolder
_calendarItems.ItemChange += new
Microsoft.Office.Interop.Outlook.ItemsEvents_ItemChangeEventHandler(Items_ItemChange);

//ItemChange event handler
void Items_ItemChange(object Item)
{
Outlook.AppointmentItem appointment = (Outlook.AppointmentItem)Item;
SetColorAppointment(appointment);
}

//changes color appointment (called within all events mentioned above)
private void SetColorAppointment(Outlook.AppointmentItem appointment)
{
int color = GetLocationColor(appointment.Location);


appointment.PropertyAccessor.SetProperty"http://schemas.microsoft.com/mapi/id/
{00062002-0000-0000-C000-000000000046}/82140003",
color);
appointment.Save();

}

When the SetColorAppointment() has been called; the event Item_ItemChange()
will be called again and again and again, because the item changed off
course! How can this behavour be solved?

I also tried this version of the SetColorAppointment() method (with
Redemption); the Items_ItemChange() will also be called again and again...

Rdo.SafeAppointmentItem safeAppointment = new Rdo.SafeAppointmentItem();
safeAppointment.Item = appointment;
int propertyID =
safeAppointment.GetIDsFromNames("{00062002-0000-0000-C000-000000000046}",
0x8214);
propertyID = propertyID | 0x3;
safeAppointment.set_Fields(propertyID, color);
//appointment.Subject = appointment.Subject;
appointment.Save();

I also tried using the Outlook.AppointmentItem.PropertyChange event. This
event fires when the Location property has been changed but when I handle
this event and try to set the colors of all appointments; the appointment
wherefrom I just changed the location still has the old location??? I checked
it and the case is that the I continuously change the color of the previous
location! During this event it seems like I don't have the current MAPIFolder
with the current appointment items?

void appointment_PropertyChange(string Name)
{
if ( Name == "Location" )
{
foreach (Outlook.AppointmentItem appointment in _items)
{
SetColorAppointment(appointment);
}
}
}

In Outlook 2003 I got it working with the Redemption method and
PropertyChange event, but this approach does not seem to work in Outlook
2007...

What is the best solution to catch the event that the location has been
changed of an appoinment and changing the color successfully in Outlook 2007?

Thank you in advance,

Johan Machielse
 
K

Ken Slovak - [MVP - Outlook]

Setting a property of an item in ItemChange() will of course cause that
event to fire again. So you need to put a flag on the item as you change the
color to show it was already changed. An alternative would be to maintain a
list of items that have been changed in that event handler and if the item
is already in the list then don't change it again.
 
J

Johan Machielse

Yep, I ready figured it out! I used a flag to check if the appointment was
already changed!

Thank you Ken!

Johan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top