Outlook 2016 AttachmentSelection is null

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

Outlook 2016 AttachmentSelection is null
 
Jeff Noble




Posts: 120
Joined: 2011-09-07
Hi,

I have code that works in Outlook (Office) 2013. I have added a custom context menu item to the right click of an attachment in a received email. The code looks like this:

 private void adxCustomSaveFileButton_OnClick(object sender, AddinExpress.MSO.IRibbonControl control, bool pressed)
        {
           AttachmentSelection attachmentSelection = null;
           attachmentSelection = control.Context as AttachmentSelection;
           ...



This code works fine in 2013, but the attachmentSelection variable is null in 2016. I am using adxnet-v703-b4061-vs-pre if that matters.
How can I get an AttachmentSelection in 2016?
Thanks,
Jeff
Posted 21 Sep, 2015 14:19:58 Top
Andrei Smolin


Add-in Express team


Posts: 14094
Joined: 2006-05-11
Hello Jeff,

I would start with checking if controlContext is not null and its type is AttachmentSelection.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 22 Sep, 2015 05:16:06 Top
Jeff Noble




Posts: 120
Joined: 2011-09-07
yeah, it is not null, and it's not an AttachmentSelection type, but I don't know what type it actually is. Here is what I do know (followed by output from the immediate window):

1. control.Context is NOT null:

control.Context
{System.__ComObject}
    Native View: To inspect the native object, enable native code debugging.
    Dynamic View: Expanding the Dynamic View will get the dynamic members for the object


2. control.Context is NOT of type AttachmentSelection:

control.Context is Microsoft.Office.Interop.Outlook.AttachmentSelection
false


3. Adding a reference to Visual Basic and testing the type of the underlying COM object says it's an Explorer, but it won't cast to one:

Microsoft.VisualBasic.Information.TypeName(control.Context)
"Explorer"
control.Context is Microsoft.Office.Interop.Outlook.Explorer
false
((Microsoft.Office.Interop.Outlook.Explorer)control.Context)
'((Microsoft.Office.Interop.Outlook.Explorer)control.Context)' threw an exception of type 'System.InvalidCastException'


Is this something you can test/confirm?

I am also having other similar issue with regions. adxOlFormsCollectionExplorerShare.GetForm(explorer); is also returning null in Outlook 2016 (but I can start another thread for that if you like).

Thanks,
Jeff
Posted 22 Sep, 2015 09:05:41 Top
Andrei Smolin


Add-in Express team


Posts: 14094
Joined: 2006-05-11
Jeff,

In what scenario do you get this? I suppose you right-click an attachment in the Reading Pane and then select your item in the context menu the IdMso of which is ContextMenuAttachments. Correct?

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 22 Sep, 2015 09:11:39 Top
Jeff Noble




Posts: 120
Joined: 2011-09-07
Andrei,

Not sure I totally follow all of that. Yes, I right click an attachment in the reading pane. I select my item in the menu which fires the click event.

this.adxCustomSaveFileButton.OnClick += new AddinExpress.MSO.ADXRibbonOnAction_EventHandler(this.adxCustomSaveFileButtonButton_OnClick);


I should note that in Outlook 2016, you don't actually right click (you can, but it just opens the menu). What I mean by this is attachments now look like a button with a drop down arrow instead of just a file that needs a right click.... Don't know if that's relevant or not...

[img]http://screencast.com/t/9aDd8fMejTQ[/img]
Posted 22 Sep, 2015 09:27:27 Top
Andrei Smolin


Add-in Express team


Posts: 14094
Joined: 2006-05-11
Thank you for the screenshot. I'll check this scenario on one of the test machines tomorrow.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 22 Sep, 2015 09:30:18 Top
Andrei Smolin


Add-in Express team


Posts: 14094
Joined: 2006-05-11
Hello Jeff,

The code below reports attachmentSelection.Count is 1 when I click my button in Outlook 2016 build 16.0.4266.1003 32bit.

private void adxRibbonButton1_OnClick(object sender, IRibbonControl control, bool pressed) {
    object context = control.Context;
    if (context == null) {
        System.Diagnostics.Debug.WriteLine("!!! adxRibbonButton1_OnClick. context == null");
        return;
    }
    string typeName = Microsoft.VisualBasic.Information.TypeName(context);
    System.Diagnostics.Debug.WriteLine("!!! adxRibbonButton1_OnClick. context is " + typeName);
    if (typeName == "Explorer") {
        Outlook._Explorer explorer = context as Outlook._Explorer;
        if (explorer == null) {
            System.Diagnostics.Debug.WriteLine("!!! adxRibbonButton1_OnClick. explorer == null");
        } else {
            System.Diagnostics.Debug.WriteLine("!!! adxRibbonButton1_OnClick. explorer != null");
            Outlook._AttachmentSelection attachmentSelection = explorer.AttachmentSelection;
            if (attachmentSelection == null) {
                System.Diagnostics.Debug.WriteLine("!!! adxRibbonButton1_OnClick. attachmentSelection == null");
            } else {
                System.Diagnostics.Debug.WriteLine("!!! adxRibbonButton1_OnClick. attachmentSelection.Count = " + attachmentSelection.Count.ToString());
                Marshal.ReleaseComObject(attachmentSelection);
            }
        }
    }
    Marshal.ReleaseComObject(context);
}



Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 23 Sep, 2015 07:53:27 Top
Jeff Noble




Posts: 120
Joined: 2011-09-07
Andrei,

Thanks for the reply. Seems that AttachmentSelection was added to the 2016 dll's for the Explorer object. I had to upgrade to the 2016 office dll's to get it to compile. It does work though. I think the other issues I am having are related to similar issues. I hope the 2016 dll's are backward compatible because I'll have to use the same references against customers running older versions of Outlook.

-Jeff
Posted 23 Sep, 2015 08:52:16 Top
Andrei Smolin


Add-in Express team


Posts: 14094
Joined: 2006-05-11
Jeff,

As far as I know, the Explorer.AttachmentSelection property was introduced in Outlook 2010. I strongly recommend using interops for the lowest Outlook version that you need to support because calling a member available in an Outlook interop but missing in the Outlook object model will end with an exception and crash. If however you need to call a member which is available in the host's version loading your add-in but missing in the interop that you use, you use late binding. See also https://www.add-in-express.com/creating-addins-blog/2010/03/16/interop-assemblies-late-binding/.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 23 Sep, 2015 09:09:28 Top