Outlook中约会和联系人的事件处理程序

本文关键字:事件处理 程序 联系人 约会 Outlook | 更新日期: 2023-09-27 18:28:35

我想将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就会被激发。(在我的程序中,首先创建联系人事件处理程序,然后创建约会事件处理程序。)

我只能创建一次EventHandlers,然后检查作为参数给定的对象的类型。但不幸的是,联系人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);
    }
    ...
}

Outlook中约会和联系人的事件处理程序

您查找的事件处理程序(ItemAddItemChangeItemRemove)在Folder级别完成。由于"日历"answers"联系人"是不同的文件夹,您无法通过一次注册同时监听这两个文件夹中的更改事件,因此每个Folder存储都需要单独的处理程序。

如果您的事件没有触发,则可能是由于作用域问题,您没有将变量myAppointmentItemsmyContactItems保留为类级变量。参见相关SO帖子。