Get Selected Attachments of MailItem

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

Get Selected Attachments of MailItem
 
Subscribe
May Wolfgang




Posts: 13
Joined: 2013-05-09

private void adxRibbonButton1_OnClick(object sender, IRibbonControl control, bool pressed)
        {
            List<Attachment> AttachmentItems = GetSelectedAttachments(control);
            MessageBox.Show(AttachmentItems.Count.ToString());
        }

        private List<Attachment> GetSelectedAttachments(IRibbonControl control)
        {
            if (control == null) return new List<Attachment>();

            object context = control.Context;
            Inspector inspector = null;
            Explorer explorer = null;
            AttachmentSelection selectedAttachments = null;
            List<Attachment> attachmentsList = new List<Attachment>();

            try
            {
                if (context is Explorer)
                {
                    explorer = context as Explorer;

                    if (explorer != null)
                    {
                        selectedAttachments = explorer.AttachmentSelection;
                    }
                }
                else if (context is Inspector)
                {
                    inspector = context as Inspector;

                    if (inspector != null)
                    {
                        selectedAttachments = inspector.AttachmentSelection;
                    }
                }
                else if (context is AttachmentSelection)
                {
                    selectedAttachments = context as AttachmentSelection;
                }

                if (selectedAttachments != null && selectedAttachments.Count > 0)
                {
                    foreach (object attachment in selectedAttachments)
                    {
                        if (attachment is Attachment)
                        {
                            attachmentsList.Add((Attachment)attachment);
                        }
                    }
                }
            }
            finally
            {
                try
                {
                    if (context != null) Marshal.ReleaseComObject(context);
                    if(explorer != null) Marshal.ReleaseComObject(explorer);
                    if (inspector != null) Marshal.ReleaseComObject(inspector);
                    if (selectedAttachments != null) Marshal.ReleaseComObject(selectedAttachments);
                }
                catch { }
            }

            return attachmentsList;
        }


This is the Initialization of the ribbon tab:

 this.adxRibbonTabAttachments.Context = "Outlook.TabSetAttachments";
            this.adxRibbonTabAttachments.Controls.Add(this.adxRibbonGroup1);
            this.adxRibbonTabAttachments.Id = "adxRibbonTab_54325b3eecb0484b82ee2e1d1342d1f2";
            this.adxRibbonTabAttachments.IdMso = "TabAttachments";
            this.adxRibbonTabAttachments.Ribbons = ((AddinExpress.MSO.ADXRibbons)((AddinExpress.MSO.ADXRibbons.msrOutlookMailRead | AddinExpress.MSO.ADXRibbons.msrOutlookExplorer)));



I have this code, but does not work(the selection is always 0) with the following scenario:
- select message
- right-click on the attachment(It seems it depends if the attachment preview was loaded one time or not)
- then click on the button from the Attachments ribbon tab

I also remarked that if I try to use the "Save As" button from the Attachment Tools, it works fine, so the selection is recognized by the Outlook.

The version that I use for tests is Outlook 2016.
Posted 10 Jun, 2019 08:21:41 Top
Andrei Smolin


Add-in Express team


Posts: 16306
Joined: 2006-05-11
Hello Wolfgang,

May Wolfgang writes:
I have this code, but does not work(the selection is always 0) with the following scenario:
- select message
- right-click on the attachment(It seems it depends if the attachment preview was loaded one time or not)
- then click on the button from the Attachments ribbon tab


As far as I can see an Explorer object is always supplied as the context object for a button located in a custom group on the TabAttachments from TabSetAttachments when Ribbons is OutlookExplorer. For such a button to return the attachment selection correctly, you should click an attachment in the UI to open the attachment preview. Even if no previewer exists for that attachment type, the attachment selection works correctly with a Ribbon button in Explorer.

If you show a button on the ContextMenuAttachments, its context object is always AttachmentSelection and it returns the selection correctly.

Hope this helps.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 11 Jun, 2019 06:46:54 Top
May Wolfgang




Posts: 13
Joined: 2013-05-09
Hello,

I do not know if you understand which is my problem.
Can you give me an email address to send a video with my scenario and to tell me than if that is correct or not ?

Thank you
Posted 18 Jun, 2019 07:31:50 Top
Dmitry Kostochko


Add-in Express team


Posts: 2839
Joined: 2004-04-05
Hi Wolfgang,

You can find our support email address in the readme.txt file located in the Add-in Express installation folder.
Posted 18 Jun, 2019 08:50:51 Top
Andrei Smolin


Add-in Express team


Posts: 16306
Joined: 2006-05-11
Hello,

I've reproduced this issue (AttachmentSelection may return zero despite the attachment selected) using a VBA macro and posted it at https://social.msdn.microsoft.com/Forums/en-US/d9742e1f-4591-4e06-86db-3d63f155aabc/attachmentselection-may-return-0-despite-an-attachment-is-selected-in-the-ui?forum=outlookdev. Once I have a possibility I'll send to the Outlook team. Once I have a result, I'll post it on the above page.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 19 Jun, 2019 05:54:11 Top