When you create your VSTO project in VS2010, the following code should be automatically generated in you ThisAddIn.cs. If not, you might want to add them in yourself.
/// <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); } private void ThisAddIn_Shutdown(object sender, System.EventArgs e) { //execute your code here, e.g. output some values to a text file }
You can place your code in ThisAddIn_Shutdown event, and executing it only when add-in is shutting down.
EDIT: Here's what MSDN says:
Starting in Outlook 2010, Outlook, by default, does not signal add-ins that it is shutting down. Specifically, Outlook no longer calls the OnBeginShutdown and OnDisconnection methods of the IDTExtensibility2 interface during fast shutdown. Similarly, an Outlook add-in written with Microsoft Visual Studio Tools for Office no longer calls the ThisAddin_Shutdown method when Outlook is shutting down.
More details in here:
http://msdn.microsoft.com/en-us/library/office/ee720183.aspx#OL2010AdditionalShutdownChanges_AddinShutdownChangesinOL2010Beta