Catching Send Button in Inspector

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

Catching Send Button in Inspector
How to catch the user clicking the Send button in an Outlook Inspector 
Pino Carafa




Posts: 81
Joined: 2016-09-28
Hello Add-in Express.... this should be a bit easier to answer than my previous topic :-)

We have a Word Add-in in which we catch the user trying to save a Word Document by adding an AddinExpress.MSO.ADXRibbonCommand to our Addin, and linking it to MSO Id FileSave

We then code _OnAction(sender As Object, control As IRibbonControl, pressed As Boolean, e As ADXCancelEventArgs) and all is well.

So I am hoping it is also possible to create one to link to the Send button in an Outlook.Inspector. If that can be done, what properties do I need to set and to what values?

I would prefer to handle the button this way rather than doing things in adxOutlookEvents.ItemSend as at that point the message is already "on its way" - and it makes the subsequent code messy and hard to control.
Posted 16 Oct, 2019 05:48:26 Top
Andrei Smolin


Add-in Express team


Posts: 16673
Joined: 2006-05-11
Hello Pino,

Pino Carafa writes:
as at that point the message is already "on its way"


Sorry? It isn't sent in that moment, it simple has come through some of the stages before sending. You can intercept the Send event of the MailItem object; this occurs on an even earlier phase. But I don't see the difference: both of them allows cancelling the send.

Anyway, the Send button isn't interceptable. I know that you can use Windows API to intercept clicking that button. What do you need to achieve?

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 16 Oct, 2019 06:46:16 Top
Pino Carafa




Posts: 81
Joined: 2016-09-28
Ok - I won't take up any of your time at this point, Andrei, as maybe I am simply doing something stupid, and I need to count out that possibility. It's a very real one haha - So I will be investigating this myself first.

This is the problem I'm having.

User is composing an email and we show a Form at the top of the Inspector. One of the things on the form is a button that allows them to pick a "case". If they do that, I use the PropertyAccessor and I set a Property on that containing a String value representing the "case code".
"http://schemas.microsoft.com/mapi/string/{00020386-0000-0000-C000-000000000046}/CaseReference}"

Here is the relevant code:


    Private Sub adxOutlookEvents_ItemSend(sender As Object, e As ADXOlItemSendEventArgs)

        Dim oItem As Outlook.MailItem = Nothing

        Try
            oItem = TryCast(e.Item, Outlook.MailItem)
            If oItem Is Nothing Then
                Exit Sub
            End If
        Catch

        End Try


and a bit further down


Dim sCaseReference As String

Dim oPA As Outlook.PropertyAccessor = Nothing
Try
    oPA = oItem.PropertyAccessor
Catch

End Try


then, when I'm confident oPA is not Nothing


Try
    Dim oProp As Object = Nothing
    Try
        oProp = oPA.GetProperty("http://schemas.microsoft.com/mapi/string/{00020386-0000-0000-C000-000000000046}/CaseReference}")
    Catch

    End Try
    If Not oProp Is Nothing Then
        Try
            sCaseReference = TryCast(oProp, String)
        Catch

        End Try
        If Not oProp Is Nothing Then
            Try
                Marshal.ReleaseComObject(oProp)
            Catch

            End Try
            oProp = Nothing
        End If
    End If
Catch

End Try


And finally

If Not oPA Is Nothing Then
    Try
        Marshal.ReleaseComObject(oPA)
    Catch

    End Try
    oPA = Nothing
End If


Now here is what's funny: When I am working in Cashed Exchange Mode, this works perfectly. I get the Property Accessor and I retrieve the CaseReference property that was set when the user "Selected a Case"

But in NON-Cashed Exchange Mode, it goes wrong. I still get the PropertyAccessor, but when I then try to get that Property, it fails

Exception thrown: 'System.Runtime.InteropServices.COMException' in KeyhouseOutlookAddin.dll
Additional information: The property "http://schemas.microsoft.com/mapi/string/{00020386-0000-0000-C000-000000000046}/CaseReference}" is unknown or cannot be found.


Here is the crazy thing.....

1) If I .Save() the mail item before getting the Property Accessor, it works. But then I create a Draft. I don't want to create a Draft....
2) If I .Copy() the mail item, and I use the PropertyAccessor on the copied item, it succeeds. But then I create a Copy of the mailitem in the Outbox.... Now I can work around that by forcibly deleting the copy after getting the info out of its PropertyAccessor, but that's hacky to say the least.

So, yes, my suspicion is that I am NOT releasing something while the user is Composing and selecting the Case. So I will investigate that part of the code myself first. If, however, you have any words of wisdom they would be greatly appreciated.
Posted 16 Oct, 2019 08:00:50 Top
Andrei Smolin


Add-in Express team


Posts: 16673
Joined: 2006-05-11
Hello Pino,

I would isolate this code in a separate add-in project, turn all other COM add-ins off and check if there's an issue in the test add-in.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 16 Oct, 2019 08:08:27 Top
Pino Carafa




Posts: 81
Joined: 2016-09-28
(I'll copy the text from the Private email for the benefit of other readers)

Ok I think this is a real issue, not something I am doing wrong.

I modified the project you sent me before for https://www.add-in-express.com/forum/read.php?PAGEN_1=1&FID=5&TID=15644#nav_start

I added an ADXOlForm to the project which is shown when the user Composes a new email. It features a single button.

On the button click I set a Property through the CurrentItem’s PropertyAccessor

Please note that there are 3 commented out bits of code where I’m releasing the objects. I tried releasing or NOT releasing the PropertyAccessor, the Mail Item and the Inspector. None of it makes the slightest bit of a difference.

I also added Private Sub AdxOutlookAppEvents1_ItemSend(sender As Object, e As ADXOlItemSendEventArgs) Handles AdxOutlookAppEvents1.ItemSend to the AddinModule.

All other Add-ins are removed c.q. disabled.

What I find is consistent with the problem I describe in https://www.add-in-express.com/forum/read.php?FID=5&TID=15668

When the Profile uses Cached Exchange Mode, everything is working perfectly. The Property is Set in the Button Click, and retrieved in the _ItemSend
When the Profile does NOT use Cached Exchange Mode, an error is raised when trying to read the Property in the _ItemSend

Regards


Pino
Posted 16 Oct, 2019 09:14:42 Top
Pino Carafa




Posts: 81
Joined: 2016-09-28

    Private Sub AdxOutlookAppEvents1_ItemSend(sender As Object, e As ADXOlItemSendEventArgs) Handles AdxOutlookAppEvents1.ItemSend

        Dim oItem As Outlook.MailItem = Nothing

        Try
            oItem = TryCast(e.Item, Outlook.MailItem)
            If oItem Is Nothing Then
                Exit Sub
            End If
        Catch

        End Try

        Dim oPA As Outlook.PropertyAccessor = Nothing
        Dim sCaseReference As String = String.Empty

        Try
            oPA = oItem.PropertyAccessor
        Catch

        End Try

        If Not oPA Is Nothing Then
            Try
                sCaseReference = oPA.GetProperty("http://schemas.microsoft.com/mapi/string/{00020386-0000-0000-C000-000000000046}/CaseReference}").ToString().Trim()
            Catch ex As Exception
                System.Diagnostics.Debug.WriteLine("Error getting Case Reference: " + ex.Message)
            End Try
            Try
                Marshal.ReleaseComObject(oPA)
            Catch

            End Try
            oPA = Nothing
        End If

        If Not String.IsNullOrEmpty(sCaseReference) Then
            System.Diagnostics.Debug.WriteLine("Retrieved Case Reference: " + sCaseReference)
        End If

        e.Cancel = True
    End Sub
Posted 16 Oct, 2019 09:31:20 Top
Pino Carafa




Posts: 81
Joined: 2016-09-28

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim oInspector As Object = Nothing
        Dim oItem As Object = Nothing

        Try
            oInspector = Me.InspectorObj
        Catch ex As Exception

        End Try

        Try
            oItem = Me.InspectorObj.CurrentItem
        Catch ex As Exception

        End Try

        Dim oPA As Outlook.PropertyAccessor = Nothing

        Try
            oPA = oItem.PropertyAccessor
        Catch

        End Try

        If Not oPA Is Nothing Then
            Try
                oPA.SetProperty("http://schemas.microsoft.com/mapi/string/{00020386-0000-0000-C000-000000000046}/CaseReference}", oItem.Subject)
            Catch ex As Exception
                System.Diagnostics.Debug.WriteLine("Error setting Case Reference: " + ex.Message)
            End Try

            'Uncommenting this makes no difference
            'Try
            '    Marshal.ReleaseComObject(oPA)
            'Catch

            'End Try
            oPA = Nothing
        End If

        'Uncommenting this makes no difference
        'Try
        '    Marshal.ReleaseComObject(oItem)
        'Catch

        'End Try

        oItem = Nothing

        'Uncommenting this makes no difference
        'Try
        '    Marshal.ReleaseComObject(oInspector)
        'Catch ex As Exception

        'End Try

        oInspector = Nothing
    End Sub
Posted 16 Oct, 2019 09:32:17 Top
Pino Carafa




Posts: 81
Joined: 2016-09-28
To summarise:

Setting the "CaseReference" always works fine. No error is ever thrown. I decided to set it to the Email Subject for this test so that we can check what, if anything, is retrieved in the _ItemSend. Commenting or Uncommenting the parts in green makes no difference; as far as I can tell it always works in Cached exchange mode, and it always throws an error when connecting to Online Exchange in NON-cached exchange mode
Posted 16 Oct, 2019 09:36:38 Top
Andrei Smolin


Add-in Express team


Posts: 16673
Joined: 2006-05-11
Does saving the item help?

PS. See you tomorrow.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 16 Oct, 2019 09:43:46 Top
Pino Carafa




Posts: 81
Joined: 2016-09-28
Thanks for your patience, Andrei,

Yes it does help but that causes the email to be saved as a Draft. I then have a new problem as the user's draft folder quickly fills up with junk ... We had a solution like that in place before and you can guess how our customers responded to our suggestion that it was their responsibility to regularly clean out their drafts folder *sigh* :-)

Indeed, talk to you tomorrow no doubt. You have the patience of a saint!
Posted 16 Oct, 2019 09:52:29 Top