首页 > 解决方案 > Outlook VSTO 正确处理 SelectionChange(当前双击崩溃插件)

问题描述

据我了解,您需要跟踪 Explorer 的激活和停用。在激活期间,您需要为当前资源管理器添加 SelectionChange 事件处理程序。这似乎非常适合单击 AppointmentItems。但是当双击一个约会系列并选择一个约会时,它会使插件崩溃。

这是来源:在课堂上

    private Outlook.Explorer currentExplorer = null;
    private Outlook.AppointmentItem currentAppointmentItem = null;

在启动中:

       currentExplorer = this.Application.ActiveExplorer();

        ((Outlook.ExplorerEvents_10_Event)currentExplorer).Activate +=
        new Outlook.ExplorerEvents_10_ActivateEventHandler(
        Explorer_Activate);

        currentExplorer.Deactivate += new
        Outlook.ExplorerEvents_10_DeactivateEventHandler(
        Explorer_Deactivate);

事件处理程序:

    void Explorer_Activate()
    {
        currentExplorer.SelectionChange += new Outlook.ExplorerEvents_10_SelectionChangeEventHandler(Selection_Change);
    }

    void Explorer_Deactivate()
    {
        currentExplorer.SelectionChange -= new Outlook.ExplorerEvents_10_SelectionChangeEventHandler(Selection_Change); ;
    }

    private void Close_Explorer()
    {

    }

    private void Selection_Change()
    {
        Outlook.MAPIFolder selectedFolder = currentExplorer.CurrentFolder;            
        if (currentExplorer.Selection.Count > 0)
        {
            Object selObject = currentExplorer.Selection[1];
            if (selObject is Outlook.AppointmentItem)
            {
                currentAppointmentItem = (Outlook.AppointmentItem)selObject;
            }
            else
            {
                currentAppointmentItem = null;
            }
        }
    }

我在看什么?注销的形式有问题吗?

标签: outlookvstooutlook-addin

解决方案


尝试将 try/catch 块添加到事件处理程序。Outlook 对象模型有时会给您带来不可预知的结果。值得添加它们并找到引发异常的位置。

currentExplorer.Selection.Count 

SelectionChange此外,您可以在NewExplorer事件中订阅该事件,并且在激活或停用资源管理器时不要在资源管理器之间切换。每当打开一个新的资源管理器窗口时都会触发该事件,无论是作为用户操作的结果还是通过程序代码。


推荐阅读