Andrew Cherry [MSFT]
Hi Tad -
I think I've established what's happening with your solution, as well as the difference between Outlook 2003 and 2007.
It's a question of reference counting. Outlook 2007 made many improvements in terms of object lifetime and memory management. For the end user, it means that when you click "Quit," in most cases Outlook will shut down, and not remain running in the background as it was wont to do in Outlook 2003 (one of the reasons why the first VSTO Application level Add-in was for Outlook). For the developer, it means that sometimes Outlook is overly aggressive about releasing objects. As evidenced by the fact that you're getting an HRESULT-based exception versus a standard exception, Outlook is being a "good" COM client; it's not destroying the object from underneath you, which almost certainly would cause an outright memory access violation, and would be a violation of the rules of COM. Instead, Outlook is orphaning the object you have a reference to, preventing a crash while at the same time allowing Outlook to shut down when the end user requests it.
The basic problem is that as far as Outlook the application is concerned, the Explorer has gone away, therefore all associated CommandBars have been released. So despite your reference to the CommandBar, the Explorer's closing releases all the associated CommandBar(s). Maintaining a reference to the Explorer window won't help either; you need to read from the CommandBar before the Explorer is destroyed.
The basic issue is when you make the read of your settings.
I have two suggestions, both using the Explorer.Close event (only off the Outlook.Explorer_10_Events interface, not Explorer):
- Set Properties.Settings.Default["xyx"] = foo.Value in the CloseEventHandler; call Save in the Shutdown.
- Cache each of the values you want to save to appropriate types; continue writing them in Shutdown as you were.
You might be able to get more specific assistance from Outlook with regards to a better option of Event to handle (maybe check to see how many Explorers are left and only save the settings on the last close ), but this should put you on the right direction.
HTH,
Andrew