Hi Julien,
I'm not sure what the problem is with returning a detailed error
string for errors. I'm not sure what you mean by 'impossible to change
the format in Excel'. If you return a double it doesn't matter if the
return type of the function is 'double' or 'object'.
You can also consider this:
* Keep your return type as double, and throw an exception if things
are wrong. This will return as #VALUE, which is consistent with how
Excel works. =IFERROR etc. work correctly.
* Register an UnhandledExceptionHandler in your AutoOpen and display
or log the errors somewhere else - maybe in the LogDisplay or a new
form you make.
The .dna file below shows how this could work.
Regards,
Govert
<DnaLibrary Name="ErrorHandler Sample" Language="CS">
<![CDATA[
using System;
using ExcelDna.Integration;
using ExcelDna.Logging;
public class MyAddIn : IExcelAddIn
{
public void AutoOpen()
{
Integration.RegisterUnhandledExceptionHandler( ErrorHandler );
}
private object ErrorHandler(object exceptionObject)
{
ExcelReference caller =
(ExcelReference)XlCall.Excel(XlCall.xlfCaller);
// Calling reftext here requires all functions to be marked
IsMacroType=true, which is undesirable.
// A better plan would be to build the reference text oneself, using
the RowFirst / ColumnFirst info
// Not sure where to find the SheetName then....
string callingName = (string)XlCall.Excel(XlCall.xlfReftext, caller,
true);
LogDisplay.WriteLine(callingName + " Error: " +
exceptionObject.ToString());
// return #VALUE into the cell anyway.
return ExcelError.ExcelErrorValue;
}
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>