Exception when calling MailItem.Reply method

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

Exception when calling MailItem.Reply method
 
Maja


Guest


Hello,

In my Outlook addin, I try to call MailItem.Reply method from WPF code behind which triggers ProcessReply event.
However, this throws exception "Operation aborted (Exception from HRESULT: 0x80004004 (E_ABORT))"

It seems that the exception happens at the moment of returning from AddinExpress.MSO.2005.dll!AddinExpress.MSO.ADXOlItemEvents_10_SinkHelper.AddinExpress.MSO.IOutlookItemEvents_10.Reply(object response, ref bool cancel) to the method where it is called.

Also, when I trigger ProcessReply not from code behind, but from Outlook Ribbon, everything seems to be working fine (of course, stack trace is different in that case)

I saw similar question about it asked here: https://www.add-in-express.com/forum/read.php?a&FID=5&TID=3144&SHOWALL_1=0, so I was wondering if you have any news on this?
Posted 13 Apr, 2021 04:57:31 Top
Andrei Smolin


Add-in Express team


Posts: 18825
Joined: 2006-05-11
Hello Maja,

Make sure you run your code on the main thread. The Click event of the Ribbon button executes on the main thread most certainly.


Andrei Smolin
Add-in Express Team Leader
Posted 13 Apr, 2021 05:25:19 Top
Maja


Guest


Hey Andrei,

code is running on the main thread.

Best,
Maja
Posted 13 Apr, 2021 06:16:20 Top
Andrei Smolin


Add-in Express team


Posts: 18825
Joined: 2006-05-11
Hello Maja,

Is that code invoked as part of an event handler of an Office event?


Andrei Smolin
Add-in Express Team Leader
Posted 14 Apr, 2021 01:48:53 Top
Maja


Guest


Hey Andrei,

it is part of the WPF event handler, more concretely, user clicks on the button (e.g. reply) on the custom WPF control and then in the event handler Reply method is called.

Best,
Maja
Posted 14 Apr, 2021 01:58:07 Top
Andrei Smolin


Add-in Express team


Posts: 18825
Joined: 2006-05-11
Hello Maja,

How can I reproduce the issue? Could you send me some code for testing? Please find the support email address in {Add-in Express installation folder}\readme.txt.


Andrei Smolin
Add-in Express Team Leader
Posted 14 Apr, 2021 02:08:03 Top
Maja


Guest


Hey Andrei,

I took some time and tweaked one of your samples to be able to reproduce the exception and sent it to support@add-in-express.com

It seems to be connected to e.Cancel = true; line that we have in the ProcessReply handler. When I remove that line, no exception happens.

Best,
Maja
Posted 19 Apr, 2021 04:28:45 Top
Andrei Smolin


Add-in Express team


Posts: 18825
Joined: 2006-05-11
Hello Maja,

Thank you for the example. I've rewritten it so that the events class isn't used at all. Thus, the exception doesn't show; this is expected since the Reply event isn't cancelled once the class isn't used. Then I wrote a couple of VBA macros connecting to the Reply event of a selected item and cancelling that event. Using these macros along with the COM add-in produces the same exception: System.Runtime.InteropServices.COMException (0x80004004): Operation aborted (Exception from HRESULT: 0x80004004 (E_ABORT)).

That is, I believe this is what you should be prepared to: some program may cancel the Reply (also ReplyAll or Forward) event and your add-in would get the exception above.

The VBA code below requires having two modules: a standard module and a class module named clsOutlookItem. By running test1, you connect to the Reply event of the selected item. The VBA code prints debug messages in the Immediate window. So, when you let your add-in invoke MailItem.Reply on the same item, you see the "clsOutlookItem ItemObj_Reply" string printed in the Immediate window and your add-in produces the exception.

Standard Module:

Option Explicit
Dim cls As clsOutlookItem

Sub test1()
Dim mail As Outlook.MailItem
Set mail = Application.ActiveExplorer().Selection.Item(1)
Set cls = New clsOutlookItem
cls.InitializeItem mail
'Stop
End Sub

' Class module named clsOutlookItem
Option Explicit

Dim WithEvents ItemObj As Outlook.MailItem

Public Sub InitializeItem(anItem As Outlook.MailItem)
    Set ItemObj = anItem
        
    Debug.Print DateTime.Now, "clsOutlookItem", "Initialize", anItem.entryId
End Sub

Public Sub Finalize()
    Debug.Print DateTime.Now, "clsOutlookItem", "Finalize"
    
    Set ItemObj = Nothing
End Sub

Private Sub ItemObj_Reply(ByVal response As Object, Cancel As Boolean)
    Debug.Print DateTime.Now, "clsOutlookItem", "ItemObj_Reply"
    Cancel = True
End Sub



Andrei Smolin
Add-in Express Team Leader
Posted 19 Apr, 2021 09:32:24 Top
Maja


Guest


Hey Andrei,

ok, thank you for looking at it. I didn't quite get it though, what is your advice on solving this problem? Rewriting the add-in so it doesn't use events class?
Also, if this is a problem inside AddInExpress, can we expect some patches?
Also, you mentioned rewriting the example? Should it be visible somewhere? Because I don't see the changes.

Best,
Maja
Posted 19 Apr, 2021 10:09:52 Top
Andrei Smolin


Add-in Express team


Posts: 18825
Joined: 2006-05-11
Hello Maja,

Sorry for the confusion. My previous post meant to inform you that the exception is expected. There may be one or more event handlers (from the same or different add-ins) listening the Reply event on a given email. And any of them could cancel the Reply event. This produces an exception to inform the caller of the Reply method (which is your add-in).

In the project you sent me the caller of the Reply method and the event handler of the Reply event were located in the same add-in. What I was trying to describe is: when testing this, I modified your project so that the event handler got located in the VBA macro, not in the add-in. Since this was a test only, no Add-in Express example would need to be rewritten.

The test demonstrated that cancelling the Reply event in the VBA macro produces the same exception in the method invoking the MailItem.Reply method.

That is, your code should be prepared to getting that exception. Once you get it, this would mean the Reply event was cancelled for reasons that probably lies outside of your add-in (say, in a macro similar to the macro above).


Andrei Smolin
Add-in Express Team Leader
Posted 19 Apr, 2021 10:29:55 Top