Outlook中的约会和联系人的EventHandler

时间:2012-03-21 13:00:15

标签: c# event-handling outlook contact appointment

我想使用EventHandlers进行约会和联系(以及以后的任务)。

我有一个ContactProgram类,它执行以下代码:

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

我有一个AppointmentPogram类,它执行以下代码:

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

如果我同时使用EventHandler,那么Appointment-EventHandler将不会触发。但是如果我注释掉创建Contact-EventHandlers的代码,那么就会触发Appointment-EventHandlers。 (在我的程序中,首先创建contact-EventHandlers,然后创建Appointment-EventHandler。)

我只能创建一次EventHandler,然后检查作为参数给出的对象的类型。但遗憾的是,联系人EventHandlers需要指向olFolderContacts和约会 - Eventhandlers olFolderCalendar。

有什么建议吗?

编辑: 我的新代码:

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

}

1 个答案:

答案 0 :(得分:3)

您寻求的事件处理程序(ItemAddItemChangeItemRemove)在Folder级别完成。由于日历和联系人是不同的文件夹 - 您无法通过单一注册同时收听更改事件 - 它需要为每个Folder商店单独处理。

如果您的事件未触发,则可能是由于存在范围问题,您没有将变量myAppointmentItemsmyContactItems保留为类级变量。请参阅related SO post