I have developed a Shared Addin for Excel using Extensibility IDTExtensibility2 interface in Visual Studio 2008.
It's quite basic in functionality. When a workbook is opened it is stored in a list of opened workbooks, when it is closed the addin will create a new text file then write out some XML to the file, this file is then read by another process which will then deserialize the XML.
The addin works under normal operations - so if the user opens and closes a file then the addin does what it should, if they quit Excel with open workbooks then it does what it should.
The problem is when the user has Excel open with open workbooks and does a Log-Off. The two methods: OnDisconnection and OnBeginShutdown don't appear to be called, at all.
I did two things to test this:
I created a TextWriterTraceListener which wrote to a log file when these two methods were called. When Excel is quit normally they are hit and information is logged in the log file, but when a user logs off there is nothing in the log file.
Inside both of these methods using File.CreateText(filename) I created a blank file. When the user quits Excel normally these files are created, but once again, when Excel is closed through a Log-Off these files aren't created.
Does anyone have any ideas how I can get around this problem? I need to capture when Excel is being closed when the user is logging off their machine...
The solution in the end was to hook into Microsoft.Win32.SystemEvents.SessionEnding
and, when this System event was fired, to manually call the OnBeginShutdown
method.