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.
Outlook VSTO add-in provokes "Would you like to save changes" dialog

Christian Regli
26
Reputation points
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:
- select an email in the explorer
- right click it and do "junk mail" --> "block user"
- 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?
Accepted answer
-
Eugene Astafiev 806 Reputation points
2022-04-20T11:28:37.803+00:00
2 additional answers
Sort by: Most helpful
-
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.
-
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); } }