3

My addin is written in c#, NetOffice, ExcelDNA using WPFframework. Some part uses winforms, too. The main UI is WPF

When there is a modal dialog displayed, users force close Excel. Next time when they launch excel, Excel will say " Excel experienced a serious problem with the '*' add-in. If you have seen this message multiple times, you should disable this add0in and checke to see if an update is available. Do you want to disable this add-in?"

Yes, No

Users usually click Yes or enter without reading the message and then my add-in disappears from Excel. So I do not want this dialog to show up. Is it possible and how? thanks

I try to catch all exception in AutoOpen() like below. But it seems have no effect to stop the dialog at all.

    public void AutoOpen()
    {           
.....
            System.Windows.Forms.Application.ThreadException += ApplicationOnThreadException;
            AppDomain.CurrentDomain.UnhandledException += CurrentDomainUnhandledException; 
            Dispatcher.CurrentDispatcher.UnhandledException += CurrentDispatcher_UnhandledException;
            TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException; 
.... 
    }


    public void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
    {
        Helper.LogError(e.Exception);            
    }

    public void ApplicationOnThreadException(object sender, ThreadExceptionEventArgs threadExceptionEventArgs)
    {
        Helper.LogError(threadExceptionEventArgs.Exception);
    }

    public void CurrentDomainUnhandledException(object sender, UnhandledExceptionEventArgs args)
    {
        if (!(args.ExceptionObject is ThreadAbortException))
        {
            Exception exc = args.ExceptionObject as Exception;               
            Helper.LogError(exc);
        }           
    }

    public void CurrentDispatcher_UnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
    {
        Helper.LogError(e.Exception);
        e.Handled = true;
    }
toosensitive
  • 2,335
  • 7
  • 46
  • 88
  • I know this is ancient but did you ever find a workaround? I'm developing a VSTO add-in and this is really a pain, while debugging at least. – Jacob H May 04 '18 at 14:32

1 Answers1

3

I presume by 'users force close Excel' you mean the user ends the Excel process from Task Manager or something.

Excel puts some internal guards in place around ribbon handler calls, so that if Excel crashes during a ribbon event handler, Excel knows which add-in was called when the crash happened, to disable next time as you describe. So if Excel is terminated unexpectedly while your modal dialog is shown, your add-in is the one remembered as the 'cause'.

Your attempt at handling unhandled exceptions is not likely to work, since .NET is hosted in a native process (in Excel). So unhandled exceptions which bubble up to Excel won't be returned to the .NET runtime, but will more likely crash the whole Excel process. So trying to handle more exceptions is not likely to help.

Perhaps a modal dialog is not the right approach, since it causes your users to get confused and think Excel has crashed, causing the unexpected termination. At least be sure to set the Excel window as the parent of the modal dialog, so that the dialog stays in front of Excel.

Govert
  • 16,387
  • 4
  • 60
  • 70
  • Thanks. Yes, users close Excel from Task Manager and by right click Excel icon & Close All windows from status bar. When data is fetched from web service, My addin shows a modal dialog which has a button "Cancel". If users want to cancel out, they can click the button. If they do not cancel, the dialog window will be in front of Excel until data fetch is done. I am using WPF, Parent of the window is read-only. I call its ShowDialog from ribbon click event handler. So I am not sure how to set its parent as Excel. – toosensitive May 09 '13 at 17:38
  • I notice when myadd is disabled. myadd com helper is put in disabledItems. myadd.xll is still in active list. So I wonder if there is a way that I can load/generate myadd com helper in AutoOpen? thanks – toosensitive May 09 '13 at 21:31
  • You could try to figure out where in the registry the disabled items are recorded, then delete those registry items from your AutoOpen. One place might be something like: HKEY_CURRENT_USER\Software\Microsoft\Office\14.0\Excel\Resiliency\DisabledItems – Govert May 10 '13 at 06:46
  • Thanks, this does not work either. I deleted registry from AutoOpen, after that Excel does not re-load the addin. It seems before AutoOpen is called, Excel already marked disabled items. – toosensitive Jun 04 '13 at 14:24