Question

I'd like to use EventHandlers for Appointments and Contacts (and later on Tasks aswell).

I have a class ContactProgram that executes the following code:

Outlook.Items myContactItems = myApplication.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts).Items; 
myContactItems.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(myContactItems_Add);
myContactItems.ItemChange += new Outlook.ItemsEvents_ItemChangeEventHandler(myContactItems_Change);
myContactItems.ItemRemove += new Outlook.ItemsEvents_ItemRemoveEventHandler(myContactItems_Remove);

And I have a class AppointmentPogram that executs the following code:

Outlook.Items myAppointmentItems = myApplication.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar).Items;
myAppointmentItems.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(myAppointmentItems_Add);
myAppointmentItems.ItemChange += new Outlook.ItemsEvents_ItemChangeEventHandler(myAppointmentItems_Change);
myAppointmentItems.ItemRemove += new Outlook.ItemsEvents_ItemRemoveEventHandler(myAppointmentItems_Remove);

If I use the EventHandler for both then the Appointment-EventHandler won't fire. But if I comment out the code that creates the Contact-EventHandlers then the Appointment-EventHandlers are fired. (In my program the contact-EventHandlers are created first and afterwards the Appointment-EventHandler.)

I could create the EventHandlers only once and then check the type of the object that is given as a parameter. But unfortunately the contact-EventHandlers eed to point to olFolderContacts and the appointment-Eventhandlers olFolderCalendar.

Any suggestions?

Edit: My new code:

...
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;
public partial class ThisAddIn
{
    private static Outlook.Application myApplication = new Outlook.Application();
    private List<OutlookContact> allContacts = new List<OutlookContact>();
    private Outlook.Folder myContactsFolder = (Outlook.Folder)myApplication.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
    private ContactProgram contactProgram;

    private List<OutlookAppointment> allAppointments = new List<OutlookAppointment>();
    private Outlook.Folder myAppointmentsFolder = (Outlook.Folder)myApplication.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);
    private AppointmentProgram appointmentProgram;

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        contactProgram = new ContactProgram(myApplication, allContacts, myContactsFolder);
        contactProgram.Start();
        appointmentProgram = new AppointmentProgram(myApplication, allAppointments, myAppointmentsFolder);
        appointmentProgram.Start();

        //initialise EventHandlers
        //myContactItems = myApplication.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts).Items;//= myContactsFolder.Items;
        Outlook.Items myContactItems = myContactsFolder.Items;
        myContactItems.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(myContactItems_Add);
        myContactItems.ItemChange += new Outlook.ItemsEvents_ItemChangeEventHandler(myContactItems_Change);
        myContactItems.ItemRemove += new Outlook.ItemsEvents_ItemRemoveEventHandler(myContactItems_Remove);

        //myAppointmentItems = myApplication.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar).Items;
        Outlook.Items myAppointmentItems = myAppointmentsFolder.Items;
        myAppointmentItems.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(myAppointmentItems_Add);
        myAppointmentItems.ItemChange += new Outlook.ItemsEvents_ItemChangeEventHandler(myAppointmentItems_Change);
        myAppointmentItems.ItemRemove += new Outlook.ItemsEvents_ItemRemoveEventHandler(myAppointmentItems_Remove);
    }

    private static void myContactItems_Add(object item)
    {
        ContactProgram.myContactItems_Add(item);
    }
    ...

}
Was it helpful?

Solution

The Event Handlers you seek (ItemAdd, ItemChange, ItemRemove) are done at the Folder level. Since Calendars and Contacts are different folders - you cannot listen to both for change events with a single registration - it requires separate handlers for each Folder store.

If your events are not firing, it is likely due to a scoping issue where you aren't keeping the variables myAppointmentItems and myContactItems as class-level variables. See related SO post.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top