Outlook VSTO add-in provokes "Would you like to save changes" dialog

Christian Regli 26 Reputation points
2022-04-20T09:42:10.817+00:00

I have a bare bone VSTO add-in. This is the entire code:

public partial class ThisAddIn
    {
        private Outlook.Explorer _currentExplorer;
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {

        }

        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
            _currentExplorer = this.Application.ActiveExplorer();
            _currentExplorer.SelectionChange += CurrentExplorer_SelectionChange;
        }

        private void CurrentExplorer_SelectionChange()
        {
            Outlook.MAPIFolder selectedFolder = _currentExplorer.CurrentFolder;
            if (_currentExplorer.Selection.Count > 0)
            {
                var selObject = _currentExplorer.Selection[1];
                if (selObject is Outlook.MailItem mailItem)
                {
                    var property = mailItem.Sender;
                }

            }

        }

        #endregion
    }

The following bug occurs with this add-in installed:

  1. select an email in the explorer
  2. right click it and do "junk mail" --> "block user"
  3. close Outlook

On closing Outlook a dialog "Would you like to save changes" pops up. (What changes ??)
If I comment out the line var property = mailItem.Sender; above, this does not occur anymore.
Also it does not seem to matter what property of the mailItem I read. It happens with pretty much every property.

Any suggestions?

Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,459 questions
{count} votes

Accepted answer
  1. Eugene Astafiev 891 Reputation points
    2022-04-20T11:28:37.803+00:00

    This doesn't happens if you release all underlying COM objects timely. So, I'd recommend starting from releasing underlying COM objects instantly using the Marshal.ReleaseComObject method. 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.

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Christian Regli 26 Reputation points
    2022-04-20T12:08:14.237+00:00

    Hi Eugene Astafiev

    Thank you for your reply.

    private void CurrentExplorer_SelectionChange()
            {
                Outlook.MAPIFolder selectedFolder = _currentExplorer.CurrentFolder;
                if (_currentExplorer.Selection.Count > 0)
                {
                    var selObject = _currentExplorer.Selection[1];
                    if (selObject is Outlook.MailItem mailItem)
                    {
                        var property = mailItem.Sender;
                        Marshal.ReleaseComObject(property );
                        property = null;
                    }
    
                }
    
            }
    

    unfortunately does not do the trick.


  2. Christian Regli 26 Reputation points
    2022-04-22T11:24:02.7+00:00

    This works !

    private void OnSelectionChange()
            {
                Selection selection = null;
                MailItem mailItem = null;
                AddressEntry adr = null;
                try
                {
                    selection = _explorer.Selection;
                    if (selection.Count == 1)
                    {
                        mailItem = selection[1] as MailItem;
                        if (mailItem != null)
                        {
                            adr = mailItem.Sender;
                        }
                    }
                }
                finally
                {
                    if (selection != null) Marshal.ReleaseComObject(selection.UnderlyingObject);
                    if (mailItem != null) Marshal.ReleaseComObject(mailItem.UnderlyingObject);
                    if (adr != null) Marshal.ReleaseComObject(adr.UnderlyingObject);
                }
            }
    
    0 comments No comments