Reputation: 5
I'm looking for a clean way to output errors in C# when using Excel with interop.
When the file is not "right" a have a message like "blabla HRESULT: 0x800AC472".
I'd rather want a message like "The file used has this or that problem". In order to do that I would need to know to what error does each HRESULT map to ?
How to get such a list of errors ?
I've look at MSDN, found some description, but I can't seem to find the list of all codes like the "0x800AC472" mention above.
Upvotes: 0
Views: 3478
Reputation: 6368
Certainly incomplete, but the common ones are:
Dim table As New Dictionary(Of Integer, String) From {
{&H800A07D0, "#NULL!"},
{&H800A07D7, "#DIV/0!"},
{&H800A07DF, "#VALUE!"},
{&H800A07E7, "#REF!"},
{&H800A07ED, "#NAME?"},
{&H800A07F4, "#NUM!"},
{&H800A07FA, "#N/A"}
}
Public Function ExcelErrorCode(code As Integer) As String
Dim result = ""
table.TryGetValue(code, result)
Return result
End Function
Upvotes: -1
Reputation: 16907
I have the Excel HRESULT code 0x800AC472 as VBA_E_IGNORE
, which indicates that Excel has 'suspended the object browser'.
Under certain circumstances Excel will reject all incoming COM requests. Two cases where this happens are where the user
There might well be other cases - for example I'm not sure what the error code is when Excel is busy calculating.
If you are doing COM automation to talk to an Excel instance that is interactively being used, then any COM call you make to Excel might return this error. One approach I take with Excel-DNA (where the interop always happens from an add-in inside the Excel process) is to attempt to call Application.Run(...) to run a macro in the add-in. Once the Application.Run call succeeds, the macro will run on the main Excel thread in a context where the COM requests won't fail due to Excel suspending the COM calls.
Some other COM errors you can expect from Excel are:
const uint RPC_E_SERVERCALL_RETRYLATER = 0x8001010A;
const uint RPC_E_CALL_REJECTED = 0x80010001; // Not sure when we get this one?
// Maybe when trying to get the Application object from another thread,
// triggered by a ribbon handler, while Excel is editing a cell.
const uint VBA_E_IGNORE = 0x800AC472; // Excel has suspended the object browser
const uint NAME_NOT_FOUND = 0x800A03EC; // When called from the main thread, but Excel is busy anyway.
Upvotes: 3