5

我正在编写一个 Outlook 加载项,并希望在保存约会之后(何时)对约会的数据做一些事情(此处不相关)。
(我是 Outlook-Addins 的新手)
所以我发现有一个AfterWrite事件,我可以在其中注册一个方法。Application上有一个ItemLoad事件。

所以我的第一个 Efford 是这样的:

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
    // ...
    this.Application.ItemLoad += 
        new Outlook.ApplicationEvents_11_ItemLoadEventHandler(atItemLoad);
}
public void atItemLoad(Object item)
{
    Outlook.AppointmentItem aitem = item as Outlook.AppointmentItem;
    if (aitem != null)
    {
        aitem.AfterWrite += 
            new Outlook.ItemEvents_10_AfterWriteEventHandler(afterWrite);
    }
}
public void afterWrite()
{
    // Who was written?
    MessageBox.Show("it was written!");
}

问题是,我不知道如何获取触发事件的约会数据。 Application.ItemLoad注册了一个获取 Object 的函数,该函数可以转换为Appointment
AfterWrite没有。我想要这样的东西:

public void afterWrite(Outlook.AppointmentItem aitem)
{
    // do something with the data from the Appointment
    MessageBox.Show(aitem.Subject + " was written!");
}

我担心我在研究完全错误的方向..

*对不起,如果我的英语一团糟-这不是我的母语

编辑:

我什至尝试过这样的构造:

private List<AppointmentEventHolder> holderList = new List<AppointmentEventHolder>();

internal class AppointmentEventHolder
{
    private Outlook.AppointmentItem aitem = null;
    public AppointmentEventHolder(Outlook.AppointmentItem item)
    {
        aitem = item;
    }

    public void onWrite()
    {
        MessageBox.Show("write: " + aitem.Subject);
    }
}

public void atItemLoad(Object item)
{
    Outlook.AppointmentItem aitem = item as Outlook.AppointmentItem;
    if (aitem != null)
    {
        AppointmentEventHolder aHolder = new AppointmentEventHolder(aitem);
        holderList.Add(aHolder);
        aitem.AfterWrite += aHolder.onWrite;
    }
}

但事件不会被解雇!我现在很沮丧

4

1 回答 1

6

好的,我想我明白了。
您必须在具有您的约会的文件夹上注册一个处理程序。

我自己阅读并体验过,保存对您注册事件的对象的引用至关重要,所以我这样做了。
这就是代码

private Outlook.MAPIFolder _CalendarFolder = null;
private Outlook.Items _CalendarItems = null;

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
    Outlook.MAPIFolder calendarFolder =
        this.Application.ActiveExplorer().Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);

    // get the Termine24-Folder (if not found, create it)
    try
    {
        _CalendarFolder = calendarFolder.Folders[Constants.FOLDERNAME];
    }
    catch
    {
        _CalendarFolder = calendarFolder.Folders.Add(Constants.FOLDERNAME);
    }
    _CalendarItems = _CalendarFolder.Items;
    attachEvents();

}
public void attachEvents()
{
    _CalendarItems.ItemAdd += Item_Add;
    _CalendarItems.ItemChange += Item_Change;
    _CalendarItems.ItemRemove += Item_Remove;
}
public void Item_Add(Object item)
{
    Outlook.AppointmentItem aitem = item as Outlook.AppointmentItem;
    if (aitem != null)
    {
        MessageBox.Show("add: " + aitem.Subject);
    }
    else
    {
        MessageBox.Show("add, but no appointment");
    }
}
public void Item_Change(Object item)
{
    Outlook.AppointmentItem aitem = item as Outlook.AppointmentItem;
    if (aitem != null)
    {
        MessageBox.Show("changed: " + aitem.Subject);
    }
    else
    {
        MessageBox.Show("change, but no appointment");
    }
}
public void Item_Remove()
{
    MessageBox.Show("Item_remove");
}

好消息是,如果约会在概览中被拖放,我现在也会收到一个事件。

但是 Item_Remove 仍然没有让我得到被删除的对象。

于 2012-07-26T10:49:38.623 回答