Outlook Inspector Active event fired twice

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

Outlook Inspector Active event fired twice
Inspector Active event is fired twice by AddInExpress 
Gutek Jakub




Posts: 8
Joined: 2017-06-06
InspectorActive event is executed twice by AddInExpress, basically once by AddInEpxress explicit, and once by interop InspectorEvents_10_ActivateEventHandler - https://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook.inspectorevents_10_event.activate(v=office.12).aspx

First InspectorActive is called during creation - DoNewInspector.

It would be good that this is documented ans this was causing that we have executed twice same code causing many issues in our plugin. This two events were executed one after another.

First event:


2017-07-10 17:40:42.2381|TRACE|XXXXXX.AddinModule|OnOutlookInspectorActive|Executed by:    at System.Environment.GetStackTrace(Exception e, Boolean needFileInfo)
   at System.Environment.get_StackTrace()
   at XXXXXX.AddinModule.OnOutlookInspectorActive(Object sender, Object inspector, String folderName)
   at AddinExpress.MSO.ADXOutlookAppEvents.DoInspectorActivate(Object inspector, String folderName)
   at AddinExpress.MSO.ADXAddinModule.OlInspectorEvents_BaseLogic.DoInspectorActivate()
   at AddinExpress.MSO.ADXAddinModule.OlInspectorsEvents_SinkHelper.DoNewInspector(Object sender, Object inspector)
   at AddinExpress.MSO.ADXAddinModule.OlInspectorsEvents_SinkHelper.AddinExpress.MSO.IInspectorsEvents.NewInspector(Object inspector)
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
   at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) 


Second event:

2017-07-10 17:40:42.5342|TRACE|XXXXXX.AddinModule|OnOutlookInspectorActive|Executed by:    at System.Environment.GetStackTrace(Exception e, Boolean needFileInfo)
   at System.Environment.get_StackTrace()
   at XXXXXX.AddinModule.OnOutlookInspectorActive(Object sender, Object inspector, String folderName)
   at AddinExpress.MSO.ADXOutlookAppEvents.DoInspectorActivate(Object inspector, String folderName)
   at AddinExpress.MSO.ADXAddinModule.OlInspectorEvents_BaseLogic.DoInspectorActivate()
   at AddinExpress.MSO.ADXAddinModule.OlInspectorEvents_10_SinkHelper.AddinExpress.MSO.IInspectorEvents_10.Activate()
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
   at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) 


IMO this is unexpected behaviour and should not be do/executed this way.

Kind regards
Posted 10 Jul, 2017 11:30:44 Top
Andrei Smolin


Add-in Express team


Posts: 14106
Joined: 2006-05-11
Hello Gutek,

This event is raised forcibly. Otherwise, you won't receive InspectorActivate if the user opens an email and uses the "Next Item" and "Previous Item| buttons to view emails in the current folder.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 11 Jul, 2017 04:33:54 Top
Gutek Jakub




Posts: 8
Joined: 2017-06-06
Ok, but then, how can we know which run is which?

My code has side effects. so executing it twice may lead in some cases (special cases) to issues. Doing if on StackTrace to not do execute code if DoNewInspector is executed fixed all issues we had.

IE.: sometimes when DoNewInspector executes we can get WordEditor for mail and sometimes we can. During second execution we can get WordEidtor always.

IMO this should be somehow distinguish between outlook event, forced event. as currently i'm blocking that using StackTract.contains method which does not look nice to have this sort of code :)
Posted 11 Jul, 2017 04:59:55 Top
Andrei Smolin


Add-in Express team


Posts: 14106
Joined: 2006-05-11
Hello Jakub (correct?),

We suppose a solution can be found on your side but we don't understand the scenario and the problems you run into. Could you please explain these in details? In particular, I suppose you've implemented some approach to deal with the same inspector (email) activated for the second time (after some other inspector was activated).

In case you prefer to discuss these in private, you can send us an email to the support email address; find it in {Add-in Express installation folder}\readme.txt. Please make sure your email contains a link to this topic.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 11 Jul, 2017 07:54:17 Top
Gutek Jakub




Posts: 8
Joined: 2017-06-06
Hi Andrei,

Yes, Jakub is correct.

On InspectorActive my code is checking mail body. It checks if some specific keyword exists at the beginning of the email. and if keyword does not exists its added. This is basically all that this method does that have side affect (text change). During InspectorActive i'm setting some mail properties too, but these values are write only - if existed, they are overridden, if not they are created.

On email reply (when user click reply in Outlook 2007) new window with email is created and InspectorActive event is fired twice. events are executed for the same Inspector. One from DoNewInspector, second from Active event.

The one executed by DoNewInspector in most cases fails when i try to get WordEditor from inspector object passed to InspectorActive event. But in some cases DoNewInspector passes inspector which contains WordEditor:


document = inspector.WordEditor as Word.Document;

if (document == null || document.ProtectionType != Word.WdProtectionType.wdNoProtection)
{
    return;
}

// sometimes we are able to reach this place



In this case, we are seeing strange behavior with our text updates. i.e.: DoNewInspector returns WordEditor but before its treated as new email, so changes to text lands in previous email (just after information from...):



-------------------------------
From: XXXXXX
Sent: Monday, July 10, 2017 3:39 PM
To: YYYYY
Subject: ZZZZZZZ

INSERTED KEYWORD



Moreover, this first event was causing that our selection (short, not exact code: WordEditor.Windows[1].Selection) was set to end of document. Not sure why, probably something with our code with this but... i don't know coz when I did in OnInspectorActive:


if (Environment.StackTrace.Contains("DoNewInspector"))
{
    return;
}


All issue had gone away. No more text inserted into previous email, no more cursor at the end of the email. It just works as it suppose to.

If you want we can go private - let me know if you need to information.

Kind regards
Posted 11 Jul, 2017 09:01:26 Top
Andrei Smolin


Add-in Express team


Posts: 14106
Joined: 2006-05-11
Hello Jakub,

Set a boolean flag in the ADXOutlookAppEvents.NewInspector event. You check the flag in the ADXOutlookAppEvents.IspectorActivate event: if the flag is set, clear the flag, and exit the event handler; do your stuff if the flag is cleared.

Will this do?

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 12 Jul, 2017 06:48:06 Top
Gutek Jakub




Posts: 8
Joined: 2017-06-06
Hi Andrei,

Not really. probably in normal application that does not "touch" Office i would say it would. But with Outlook and Inspector (including issue with new mail window as dialog - events are not wired up for Inspector when you send mail from right click on file, as well as from other office application) i would say there still be a case when this will not work - and i prefer working all the time (StackTrace.IndexOf) then something that might cause issue for a some users. It already wasn't something easy to reproduce and catch DoNewInspector, but it did happened. So if you will take enoguh users you will have cases where flag will not work - especially that events are having some time between them.

Kind regards
Posted 12 Jul, 2017 15:31:48 Top
Andrei Smolin


Add-in Express team


Posts: 14106
Joined: 2006-05-11
Hello Jakub,

Another variant. You can get the Inspector parameter in the ADXOutlookAppEvents.NewInspector event, cast it to Outlook.Inspector and connect to its Inspector.Activate event directly. Still, this approach requires accuracy. You should understand that both you and Add-in Express connect to this Inspector object: you must NOT release it as Add-in Express relies on its state. You detach from that inspector in the ADXOutlookAppEvents.InspectorClose event: compare if the inspector being closed is your inspector.

What do you think?

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 13 Jul, 2017 10:35:42 Top