Outlook VSTO Handling SelectionChange correctly (currently doubleclick crashes Addin)

好久不见. 提交于 2020-05-17 05:56:08

问题


From what I understand you need to track Activation and Deactivation of the Explorers. During activation, you need to add SelectionChange event handlers for the current explorer. This seems to work perfectly for single clicks on AppointmentItems. But it crashes the Addin when double-clicking on an appointment series and selecting a single Appointment.

Here is the source: On class level

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

within Startup:

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

The event handlers:

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

What am I overlooking? Is the form of deregistering a problem?


回答1:


Try to add try/catch blocks to the event handlers. The Outlook object model can give you unpredictable results sometimes. It is worth adding them and find where an exception is thrown.

currentExplorer.Selection.Count 

Also, you may subscribe to the SelectionChange event in the NewExplorer event and don't switch between explorers when they are activated or deactivated. The event is fired whenever a new explorer window is opened, either as a result of user action or through program code.




回答2:


The only thing which I added was a handler for NewInspector and InspectorClose events along with Marshal.ReleaseComObject(). The only thing which I can imagine that double clicking while debugging I got in some kind of race condition (because double clicking also triggers the Selection_Change event). But this is only a guess.




回答3:


You do not need to add and remove event handlers as an explorer is activated / deactivated. Are you trying to support multiple explorers? In that case, create a wrapper class that hold the Explorer object as it member and uses its methods as event handlers.



来源:https://stackoverflow.com/questions/61694719/outlook-vsto-handling-selectionchange-correctly-currently-doubleclick-crashes-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!