Question

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:

  1. 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.

  2. 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...

Was it helpful?

Solution

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.

OTHER TIPS

This used to cause a methode ~ of object ~ failed error in VB6 days.

Try WorkbookBeforeClose or potentially ProtectedViewWindowBeforeClose.

One issue you may have with these if I remember correctly, is that you can't capture when if the event is cancelled, so if you're using this to clean up, I believe you need to also do some work in one of the activate or open events such that your addin will be useable if the user cancels the closing action....

Hope this makes sense.

I had the same problem with my Outlook 2010 addin. It may be something to do with the fact that Outlook 2010 does not signal add-ins that it is shutting down.

Specifically, Outlook [2010] 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.

If you still want your addin to be notified when Outlook 2010 is shutting down (as I did), you need to latch on to the Application's ApplicationEvents_Event_Quit event, using code like mine below (your shutdown code should still run in both the OnDisconnection and OnBeginShutdown methods, in any case):

public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref System.Array custom)
{
    // As this is an Outlook-only extension, we know the application object will be an Outlook application
    _applicationObject = (Microsoft.Office.Interop.Outlook.Application)application;

    // Make sure we're notified when Outlook 2010 is shutting down
    ((Microsoft.Office.Interop.Outlook.ApplicationClass)_applicationObject).ApplicationEvents_Event_Quit += new ApplicationEvents_QuitEventHandler(Connect_ApplicationEvents_Event_Quit);
}

private void Connect_ApplicationEvents_Event_Quit()
{
    Array emptyCustomArray = new object[] { };
    OnBeginShutdown(ref emptyCustomArray);
}

public void OnDisconnection(Extensibility.ext_DisconnectMode disconnectMode, ref System.Array custom)
{
    addinShutdown();
}

public void OnBeginShutdown(ref System.Array custom)
{
    addinShutdown();
}

private void addinShutdown()
{
    // Code to run when addin is being unloaded, or Outlook is shutting down, goes here...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top