문제

I am exporting data to Excel from within my C# application, using the Microsoft.Office.Interop.Excel library.

Calling sheet.Cells[currentRow, 1]) with an invalid currentRow value, results in a System.Runtime.InteropServices.COMException with the message:

"Exception from HRESULT: 0x800A03EC"

and the ErrorCode -2146827284

However, I've received the exact same errorcode for ordering Excel to save to a write protected file.

Is there a way for me to distinguish between the COMExceptions received from Excel, so I can handle them accordingly? I would rather not leave my users with "Something went wrong while working with Excel, sorry, sucker."

도움이 되었습니까?

해결책

I've been working with Excel interop for several years and I have not found a way. Your best bet is to check for potential issues before they occur. I wish there was something better, but to the best of my knowledge there isn't.

EDIT
You can trap different errors within VBA, so Excel itself does distinguish between the various errors, but I believe that .NET roles all of these errors up into a single COMException class which gives you no detailed information.

I found this article detailing the VBA error codes:
http://support.microsoft.com/kb/146864

다른 팁

The whole point of a COMException is that it's coming back from the COM side of things. Usually, in COM, error conditions are signalled by the HRESULT return value from a particular method not being 0.

That, unfortunately, is the extent of error information built into the COM "contracts". So you get the error value (0x800A03EC is the same value as -2146827284). The .NET side of things has no further information.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top