locked
Outlook - Command "CopyAttach" - how to find out what to copy? RRS feed

  • Question

  • Hello!

    I want to intercept the copy to clipboard process of attachments in Outlook. I discovered that the command "CopyAttach" does the job for the ribbon controls and redirected it to my method. Unfortunately with the parameters delivered I cannot find out which attachment was selected.

    Here the code of the method which tries to find out which attachment was ment:

            public void Ribbon_CopyAttach(Office.IRibbonControl control, ref bool cancelDefault)
            {
                Outlook.Explorer expl = control.Context as Outlook.Explorer;
                if (expl != null)
                {
                    int attSel = expl.AttachmentSelection.Count; // is 0
                    int sel = expl.Selection.Count; // is 1
                    Outlook.MailItem selItem = expl.Selection[1] as Outlook.MailItem;
                    //But which attachment is selected ?
                }
                else
                {
                    cancelDefault = false;
                }
            }

    Any hint is appreciated :)

    Many thanks,

    Harald

    Tuesday, October 4, 2016 4:06 PM

Answers

  • You need to handle Explorer.AttachmentSelectionChange event and read the Explorer.AttachmentSelection property in your AttachmentSelectionChange  event handler . 

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    • Marked as answer by Hago67 Monday, October 10, 2016 9:36 AM
    Tuesday, October 4, 2016 4:11 PM

All replies

  • You need to handle Explorer.AttachmentSelectionChange event and read the Explorer.AttachmentSelection property in your AttachmentSelectionChange  event handler . 

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    • Marked as answer by Hago67 Monday, October 10, 2016 9:36 AM
    Tuesday, October 4, 2016 4:11 PM
  • Hello Dmitry,

    thanks for the quick response and the hint.

    Unfortunately your solution does not always work :(

    Here the two methods I wrote:

            private void Window_AttachmentSelectionChange()
            {
                System.Diagnostics.Debug.WriteLine("Entering Window_AttachmentSelectionChange()");

                if (m_Window.Selection.Count > 0)
                {
                    System.Diagnostics.Debug.WriteLine("--- m_Window.Selection contains:");
                    foreach(Outlook.MailItem mail in m_Window.Selection)
                    {
                        string msg = "--- ";
                        msg += "\"" + mail.Subject + "\"";
                        System.Diagnostics.Debug.WriteLine(msg);
                    }
                }
                else
                {
                    System.Diagnostics.Debug.WriteLine("--- m_Window.Selection.Count is 0");
                }

                if (m_Window.AttachmentSelection.Count > 0)
                {
                    System.Diagnostics.Debug.WriteLine("--- m_Window.AttachmentSelection contains:");
                    foreach(Outlook.Attachment att in m_Window.AttachmentSelection)
                    {
                        string msg = "--- ";
                        msg += att.FileName;
                        System.Diagnostics.Debug.WriteLine(msg);
                    }
                }
                else
                {
                    System.Diagnostics.Debug.WriteLine("--- m_Window.AttachmentSelection.Count is 0");
                }
                
                m_LastAttachmentSelection = m_Window.AttachmentSelection;

                System.Diagnostics.Debug.WriteLine("Leaving Window_AttachmentSelectionChange()");
            }

            public void Ribbon_CopyAttach(Office.IRibbonControl control, ref bool cancelDefault)
            {
                System.Diagnostics.Debug.WriteLine("Entering Ribbon_CopyAttach");

                if (control.Context is Outlook.Explorer)
                {
                    System.Diagnostics.Debug.WriteLine("--- control.Context is Outlook.Explorer");
                    Outlook.Explorer expl = control.Context as Outlook.Explorer;
                    OutlookExplorer wrap = ThisAddIn.FindOutlookExplorer(expl);

                    System.Diagnostics.Debug.WriteLine("--- Selected attachments found:");
                    foreach(Outlook.Attachment item in wrap.m_LastAttachmentSelection)
                    {
                        System.Diagnostics.Debug.WriteLine("--- * " + item.FileName);
                    }
                }
                else if (control.Context is Outlook.Inspector)
                {
                    System.Diagnostics.Debug.WriteLine("--- control.Context is Outlook.Inspector");
                    Outlook.Inspector insp = control.Context as Outlook.Inspector;

                    System.Diagnostics.Debug.WriteLine("--- Selected attachments found:");
                    foreach (Outlook.Attachment item in insp.AttachmentSelection)
                    {
                        System.Diagnostics.Debug.WriteLine("--- * " + item.FileName);
                    }
                }
                else
                {
                    System.Diagnostics.Debug.WriteLine("--- Passing request to default handler");
                    cancelDefault = false;
                }

                System.Diagnostics.Debug.WriteLine("Leaving Ribbon_CopyAttach");
            }

    With the following steps it does not work:

    1. Start Outlook 2016
    2. Select mail with attachments in Inbox (in my case mail has subject "Video")
    3. Select one attachment in the mail -> Ribbon switches to "Attachments"
    4. Right-click the attachment - Context menu "Copy"

    Below you find the output of the debug messages:

    Entering Window_AttachmentSelectionChange()
    --- m_Window.Selection contains:
    --- "Video"
    --- m_Window.AttachmentSelection.Count is 0
    Leaving Window_AttachmentSelectionChange()
    Entering Ribbon_CopyAttach()
    --- control.Context is Outlook.Explorer
    --- Selected attachments found:
    Leaving Ribbon_CopyAttach()

    You can see that the call to Window_AttachmentSelectionChange() does not contain the selected attachment which results in a failure in Ribbon_CopyAttach().

    That only happens for the first selection. If after the first click into the attachments a different attachment or an additional one is selected the call to Window_AttachmentSelectionChange() carries the correct selection.

    Can you reproduce that or have you ever heared of that?

    Many thanks,

    Harald


    • Edited by Hago67 Wednesday, October 5, 2016 3:10 PM
    Wednesday, October 5, 2016 3:09 PM
  • Sorry, I forgot that AttachmentSelection only works from the Application.AttachmentContextMenuDisplay event.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Wednesday, October 5, 2016 6:50 PM
  • AttachmentSelection works - except for the first selection only. If I select different attachments in the same mail they all pop up in AttachmentSelection.

    Seems to me like an error in Outlook.


    • Edited by Hago67 Thursday, October 6, 2016 5:10 PM
    Thursday, October 6, 2016 5:08 PM
  • You might want to open a support case with Microsoft if you want to have it fixed...

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Thursday, October 6, 2016 5:38 PM
  • First of all, I don't know how to do this. Additionally I believe I read in this forum that this is combined with costs - is that true?

    Second, i am wondering that it's only me who is stumbling over this issue. Maybe others have already solved it or know more about it - like you did above.

    Finally I don't know if this is a issue only I am experiencing or if others can reproduce it.


    I am also wondering how the "default code" is determining which attachment to copy. Because it does it correct. :(
    • Edited by Hago67 Thursday, October 6, 2016 6:52 PM
    Thursday, October 6, 2016 6:49 PM
  • If you have MSDN subscription, you get 3 or 4 free incidents a year. If it turns out to be a bug in Outlook, the fee will be refunded.

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Thursday, October 6, 2016 6:53 PM
  • Hi Hago67,

    According to your description, if you have any feedback for Outlook, please feel free to submit them to User Voice:

    https://outlook.uservoice.com/

    If this issue is urgent, I suggest you contacting Microsoft support to raise an incident so that our engineer could work closely with him to identify the root cause and resolve this issue as soon as possible.
    If the support engineer determines that the issue is the result of a bug the service request will be a no-charge case and you won't be charged. 

    Please visit the below link to see the various paid support options that are available to better meet your needs. 

    https://msdn.microsoft.com/subscriptions/bb266240.aspx

    Thanks for your understanding.
    Friday, October 7, 2016 7:43 AM
  • Hi David,

    thanks for your reply.

    Do you really think that User Voice is the right environment to discuss whether the API Explorer.AttachmentSelectionChange is always working correctly or not?

    If yes, then Microsoft starts to mix up developers with end-users :)

    Thanks for your understanding.



    • Edited by Hago67 Friday, October 7, 2016 1:39 PM
    Friday, October 7, 2016 11:20 AM
  • I just made a test with Outlook 2013 and there Explorer.AttachmentSelectionChange works correctly - in all situations.

    It seems this error was newly created in Outlook 2016 !

    Friday, October 7, 2016 1:36 PM
  • No, if you really need a reply from somebody who can tell you if there is a problem and tell you when/if it will be fixed, you need to open a support ticket.

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Friday, October 7, 2016 1:38 PM
  • Hi Dmitry,

    as Explorer.AttachmentSelectionChange works fine with Outlook 2013 your suggestion is correct :)

    Thanks for your help and your time.

    Monday, October 10, 2016 9:38 AM
  • Hi David,

    I have 3 types of incidents I can open:

    • product support incident
    • advisory service request
    • program support case

    Which one is the right for this kind of error?

    Thanks,

    Harald

    Monday, October 10, 2016 12:36 PM
  • For people who might be interested:

    Today I installed a new test machine with Outlook 2016. After installation of my AddIn I could see that it works correctly as with Outlook 2013. I checked the versions of Outlook and found out that on the new machine I have 16.0.4266.1003 (working correctly) and in my development environment I have 16.0.7167.2060 (not working correctly).

    Unfortunately I could not find a possibility to step back to the older version.

    Monday, October 10, 2016 3:09 PM