0

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.

Boby2000
  • 5
  • 1
  • 4
  • something like these? http://msdn.microsoft.com/en-us/library/cc704587.aspx – Nyra Jun 05 '14 at 20:18
  • it also seems you can create custom ones so if it does not exist maybe it is defined elsewhere in code: http://msdn.microsoft.com/en-us/library/windows/desktop/ms694497.aspx – Nyra Jun 05 '14 at 20:19
  • Do you have specific code to post where the error occurs? – Jimmy Smith Jun 05 '14 at 20:46
  • 2
    HRESULT codes that start with 0x800A are internal errors in the application that you are interoperating with. The last 4 digits are the hex code of the internal error. So in your case that's 0xC472 == "Excel error 50290". That gives your user a hundred thousand Google hits. "The file used has this or that problem" gives your user no Google hits, Microsoft does not maintain a mapping of internal error codes to error descriptions and Google doesn't maintain a list of your error descriptions. Use Marshal.GetHRForException() – Hans Passant Jun 05 '14 at 21:04
  • No I do not have a sample of code sorry, it just happened when someone run my tool and I can't seem to reproduce it. That's a reason why I wanted to create nicer error messages – Boby2000 Jun 07 '14 at 01:01
  • Thanks Hans that is good to know... I guess wanting nicer messagers isn't always the right way to do it.. – Boby2000 Jun 07 '14 at 01:03

1 Answers1

3

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

  • is busy editing a formula, or
  • presses down the mouse button on the Excel sheet.

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.
Govert
  • 16,387
  • 4
  • 60
  • 70
  • The two points given might be the reason. Where did you get those const unit ? I've been looking on Google and I wasn't able to find i... – Boby2000 Jun 07 '14 at 01:09
  • It's just from the Excel-DNA code. The names were salvaged from different places over the years - I don't know of any authoritative reference. – Govert Jun 07 '14 at 12:16