adxOutlookAppEvents_ExplorerSelectionChange triggers twice

Add-in Express™ Support Service
That's what is more important than anything else

adxOutlookAppEvents_ExplorerSelectionChange triggers twice
adxOutlookAppEvents_ExplorerSelectionChange triggers twice mail selection 
Omkar Deshpande


Guest


Hi Andrei,

In my add-in I want to capture REply/Reply All/ FWD events and for that I have created adxOutlookAppEvents and using events such as adxOutlookAppEvents_ExplorerSelectionChange, adxOutlookAppEvents_InspectorActivate etc(adxOutlookAppEvents_ExplorerActivatewas used as well but now commented it.). to call ConnectTo on OutlookMailItemEvents object.
When I click on any of the email from inbox, I see two calls to the adxOutlookAppEvents_ExplorerSelectionChange method.
What could be wrong that is causing this issue?

Another problem I am facing is, We have one more COM based add-in(not based on add-in express) which on email selection shows the pop-up message asking for credentials(with OK and cancel option), this popup comes only once for a mail and based on user input, sensitive information from mail is shown. But due to this double call(or may be some other issue in my add-in express code) the popup keeps coming continuously. If I disable my add-in or check the previous message entryID(to decide if event is generated for same selected mail) then other add-in works perfectly.
I am facing few more issue for other add-in from the day I have used ConnectTo method in my new add-in express based add-in.

Can you please check code and let me know If I have done anything wrong. I have used https://www.add-in-express.com/creating-addins-blog/2011/09/06/outlook-item-events/ as a reference.

private void ConnectToSelectedItem(object explorer)
{
Outlook.Selection sel = null;

try
{
if (explorer != null)
{
Outlook._Explorer expl = explorer as Outlook._Explorer;
if (expl != null)
{
sel = expl.Selection;
if (sel.Count == 1)
{
object item = sel[1];
if (item is Outlook._MailItem)
{
Outlook._MailItem mynewitem= item as Outlook._MailItem;
VSLogger.log.Debug("Selelected item is mailItem.");
if (OutlookMailItemEvents != null)
{
Outlook._MailItem mymailItem=OutlookMailItemEvents.ItemObj as Outlook._MailItem;
VSLogger.log.Debug("In if block.");
if (mymailItem == null || (mynewitem !=null && !(String.Equals(mymailItem.EntryID,mynewitem.EntryID, StringComparison.OrdinalIgnoreCase))))
{
OutlookMailItemEvents.ConnectTo(item, true);
VSLogger.log.Debug("OutlookMailItemEvents.ConnectTo called.");
}
else
VSLogger.log.Debug("Mail is same as selected one.");
}

}
else
{
Marshal.ReleaseComObject(item);
VSLogger.log.Debug("Selelected item is not mailItem.");
}
}
}
else
VSLogger.log.Debug("explorer object is not of type Outlook._Explorer, can not be type casted.");
}
else
VSLogger.log.Debug("explorer object is null");
}
catch(Exception ex)
{
VSLogger.log.Debug("Exception occured while converting object to Outlook._Explorer. Exception : " + ex.ToString() + " StackTrace: " + ex.StackTrace);
}
finally
{
if (sel != null)
Marshal.ReleaseComObject(sel);
}
}


private void adxOutlookAppEvents_ExplorerSelectionChange(object sender, object explorer)
{
VSLogger.log.Info("adxOutlookAppEvents_ExplorerSelectionChange Called.");
ConnectToSelectedItem(explorer);
}


Logs printed:

02-18-2020 15:30:37.600 [INFO ] XXX.AddinModule.adxOutlookAppEvents_ExplorerSelectionChange : adxOutlookAppEvents_ExplorerSelectionChange Called.
02-18-2020 15:30:37.601 [DEBUG] XXX.AddinModule.ConnectToSelectedItem : Selelected item is mailItem.
02-18-2020 15:30:37.601 [DEBUG] XXX.AddinModule.ConnectToSelectedItem : In if block.
02-18-2020 15:30:37.619 [DEBUG] XXX.AddinModule.ConnectToSelectedItem : OutlookMailItemEvents.ConnectTo called.
02-18-2020 15:30:37.628 [INFO ] XXX.AddinModule.adxOutlookAppEvents_ExplorerSelectionChange : adxOutlookAppEvents_ExplorerSelectionChange Called.
02-18-2020 15:30:37.629 [DEBUG] XXX.AddinModule.ConnectToSelectedItem : Selelected item is mailItem.
02-18-2020 15:30:37.629 [DEBUG] XXX.AddinModule.ConnectToSelectedItem : In if block.
02-18-2020 15:30:37.630 [DEBUG] XXX.AddinModule.ConnectToSelectedItem : Mail is same as selected one.


Thanks,
Omkar
Posted 18 Feb, 2020 04:46:35 Top
Andrei Smolin


Add-in Express team


Posts: 18830
Joined: 2006-05-11
Hello Omkar,

Omkar Deshpande writes:
When I click on any of the email from inbox, I see two calls to the adxOutlookAppEvents_ExplorerSelectionChange method. What could be wrong that is causing this issue?


This is the way Outlook works and your code should be prepared to this.

Omkar Deshpande writes:
Can you please check code and let me know If I have done anything wrong


First off, sel = expl.Selection should be wrapped in a try/catch block as this call produces an exception when in some Outlook folders such as RSS Feeds. The list of folders producing that exception depends on the Outlook version. Also, extra details may be involved. You must be prepared to that exception anyway.

To explain the other issue, consider this line:

object item = sel[1];


It creates a COM object by accessing the Outlook.Selection.Item() method and creates the item variable holding that COM object. The COM object must be released if you do not connect to its events. But that code releases that variable only if the item selected is not a MailItem! Modify it to fulfill this requirement.

More comments.

Outlook._MailItem mynewitem = item as Outlook._MailItem;


This line creates a new variable pointing to the same COM object as item. From the practical point of view it doesn't matter which of the variables you release. From the methodical view point, I'd suggest releasing item; this isn't a strict requirement of course.

Outlook._MailItem mymailItem=OutlookMailItemEvents.ItemObj as Outlook._MailItem;


This line creates a variable pointing to the COM object used by the OutlookMailItemEvents class. You shouldn't release this COM object because this will stop that class or break it down.

Omkar Deshpande writes:
adxOutlookAppEvents_ExplorerActivate


This event also fires when that add-in closes its message box. As soon as it is done, Windows activates the window beneath the message box.

Omkar Deshpande writes:
If I ... check the previous message entryID(to decide if event is generated for same selected mail)


I assume the code above demonstrates this logic, correct?


Andrei Smolin
Add-in Express Team Leader
Posted 18 Feb, 2020 05:44:52 Top