6
votes

I have created a Outlook addin for selected attachment to get details of the attachment. and its working fine in Outlook 2010. But when i build it for outlook 2016, then it becomes null.

Below is the code in ThisAddIn.cs:-

 private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            System.Reflection.Assembly assemblyInfo = System.Reflection.Assembly.GetExecutingAssembly();
            Uri uriCodeBase = new Uri(assemblyInfo.CodeBase);
            string Location = Path.GetDirectoryName(uriCodeBase.LocalPath.ToString());
            var path = Location.Split(new string[] { "bin" }, StringSplitOptions.RemoveEmptyEntries);
            var rootDir = path[0].ToString();
            var forPermissionsRootDirectory = Path.GetDirectoryName(rootDir);
            SetPermissions(forPermissionsRootDirectory);

            app = this.Application;
            app.AttachmentContextMenuDisplay += new Outlook.ApplicationEvents_11_AttachmentContextMenuDisplayEventHandler(app_AttachmentContextMenuDisplay);//attach Attachment context Menu Event//

        }

 void app_AttachmentContextMenuDisplay(Office.CommandBar CommandBar, Outlook.AttachmentSelection selection)
        {
            selectedAttachment = selection;
            RibbonUI.InvalidateControlMso("ContextMenuAttachments");//will get XML file data//

        }

and this is the code in AttachmentContextMenu.cs:-

public void OnOpenMyMotionCalendarButtonClick(Office.IRibbonControl control)
        {
            Outlook.AttachmentSelection selection = ThisAddIn.selectedAttachment;
             if ((selection.Count > 0))
                {
                   //My further working
                }
         }

In the selection , there is always null for outlook 2016. Please suggest what to do?

Kind Regards, Ariel

1
Did you get a fix for this?Pooran
did you find out the cause ? was this solved ?Stavm

1 Answers

0
votes

I believe Outlook developers added an extra logic for releasing the passed as a parameter object (attachment). So, you need to gather all the required information in the event handler because as soon as the method ends the object can be destroyed. Nobody can guarantee that objects are alive after the event handlers were fired. Do you get a valid object in the AttachmentContextMenuDisplay event handler?

All Outlook add-ins should systematically release their references to Outlook objects when they are no longer needed. Use System.Runtime.InteropServices.Marshal.ReleaseComObject to release an Outlook object when you have finished using it. Then set a variable to Nothing in Visual Basic (null in C#) to release the reference to the object. Read more about that in the Systematically Releasing Objects article.