You can override the virtual function CXllApp::DisplayException() to change the exception-handling behavior of all functions in the add-in.
In particular, you may wish to convert all instances of a particular
class of exception to a particular Excel error value, such as #VALUE!.
The library function CXllApp::ConvertTypeToError
can be used to do the hard work, along with the
XLEXCEPTION_TRANSLATION
macro.
CopyC++XlExceptionTranslation translations[] = {
XLEXCEPTION_TRANSLATION(CXlConversionException, xlerrValue),
XLEXCEPTION_TRANSLATION_END
};
void CTutorial1App::DisplayException(CXlOper& xloResult, const CXlRuntimeException& ex)
{
// Try the conversion list
if (ConvertTypeToError(translations, xloResult, ex))
return;
// Revert to the base class for the standard handler
CXllApp::DisplayException(xloResult, ex);
}
This macro defines a translation pair containing:
The macro will cause a compiler error if the class name is misspelt; this behavior is intentional and avoids the possibility of errors that might occur if you merely provide the name of the exception type as a string.
If you use the XLEXCEPTION_TRANSLATION macro, you should ensure that your project
has the Enable Run-time Type Info setting set to Yes (/GR).
The setting can be found on the C/C++ - Language page of the project settings.
You may want to change the default format of error messages, which is to wrap the exception's error message as follows:
#ERROR: [message]
The code below changes the format to #VALUE! [message].
CopyC++void CTutorial1App::DisplayException(CXlOper& xloResult, const CXlRuntimeException& ex) { CString fullMessage; fullMessage.Format("#VALUE! %s", ex.what()); xloResult = fullMessage; }