2

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);
    }
    ...

}
joma
  • 114
  • 2
  • 6

1 Answers1

4

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.

Community
  • 1
  • 1
SliverNinja - MSFT
  • 31,051
  • 11
  • 110
  • 173
  • Hey SliverNinja, thank you for your quick answer. I moved my code as indicated in the SO post (see above). I think I'm already having separate handlers for each Folder store, haven't I? The problem still persists. In the beginning the EventHandlers fire but later on not any more. Any help would be appreciated. – joma Mar 21 '12 at 15:31
  • Does anybody have an idea? Can I provide you more information about the problem? – joma Apr 16 '12 at 10:03
  • 3
    `Outlook.Items myContactItems` and `Outlook.Items myAppointmentItems` need to be members of your class - don't declare them inside of `ThisAddIn_Startup` or they will only fire once until GC'd. If your events are only fired once - it's guaranteed to be a scope issue with your variables getting GC'd. – SliverNinja - MSFT Apr 16 '12 at 12:44