Error Handling with registered Exception Handlers

Oct 30, 2014 at 4:05 PM
Dear all,

we're facing the problem that we want to show more specific error messages in Excel than the ordinary #VALUE.
The solution provided by ExcelDNA in the ErrorHandler dna-file works fine for us, if we include the code in our dna-file:
<DnaLibrary RuntimeVersion="v4.0" Name="DVforExcel" Language="CS">
    <ExternalLibrary Path=".\bin\Debug\ExcelInterface.dll" />
<![CDATA[
using System;
using System.Text;
using ExcelDna.Integration;

public class MyAddIn : IExcelAddIn
{

    public void AutoOpen()
    {
        ExcelIntegration.RegisterUnhandledExceptionHandler( ErrorHandler );
    }

    private object ErrorHandler(object exception)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("!!! EXCEPTION: ");
        sb.Append(exception.ToString());

        return sb.ToString();
    }

    public void AutoClose()
    {
    }

    [ExcelFunction(IsMacroType=true)]
    public static double DoGood()
    {
        return 7;
    }

    [ExcelFunction(IsMacroType=true)]
    public static double DoBad()
    {
        throw new InvalidOperationException("Don't be evil!");
   
 }
}
]]>
</DnaLibrary>
However we'd like to incorporate this into the code we compile to our dll and hence created a cs-file in our project containing the exact same code as above. Unfortunately, this doesn't seem to work, as in that case we keep seeing the #VALUE errors.
Does anyone have a clue what causes this behaviour?

Thanks in advance,
all the best,
Gero
Coordinator
Oct 30, 2014 at 6:46 PM
Hi Gero,

I expect it to work without problems in your compiled library.
You might want to be sure that the AutoOpen is really running, by writing to a log, showing an alert or something.

Perhaps if you have an extra copy of ExcelDna.Integration.dll next to your .xll file (you don't need one, the right copy is embedded inside the .xll) then the loading can go wrong due to the types not being detected.

Otherwise, start from a simple Class Library project, add the Excel-DNA NuGet package, and paste in your code. It should work, and you can get back to the full add-in from there.

-Govert