Teerasej
Teerasej

Reputation: 1536

Excel error HRESULT: 0x800A03EC while trying to get range with cell's name

I am working with Window Service project. that have to write data to a sheet in Excel file in a sequence times.

But sometimes, just sometimes, the service throw out the exception "Exception from HRESULT: 0x800A03EC" while it's trying to get range with cell's name.

I have put the code of opening excel sheet, and getting cell here.

1: Opening excel sheet

m_WorkBook = m_WorkBooks.Open(this.FilePath, 0, false, 5,
     "", "", true, Excels.XlPlatform.xlWindows, ";",
     true, false, 0, true, 0, 0);

2: Getting cell to write

protected object m_MissingValue = System.Reflection.Missing.Value;
Range range = m_WorkSheet.get_Range(cell.CellName, m_MissingValue);
// error from this method, and cell name is string.

Upvotes: 31

Views: 148918

Answers (16)

dotNET
dotNET

Reputation: 35400

For me this was happening because I was doing this:

System.Windows.Forms.Clipboard.SetDataObject(SomeSerializedObject);
ws.Range[selectionAddress].Select();
ws.Paste();

For some reason, Excel needs some "breathing time" before we could issue Paste command. After spending a lot of time digging this cryptic error, I found this reason and added the following delay mechanism:

System.Windows.Forms.Clipboard.SetDataObject(SomeSerializedObject);
ws.Range[selectionAddress].Select();
Dispatcher.CurrentDispatcher.BeginInvoke(new System.Action(() =>
{
  ws.Paste();
}));

and it started behaving correctly. Note that I was using WPF and had to use BeginInvoke through the current Dispatcher object. It's basically running the Paste() function asynchronously to free up Excel application to do whatever it need to do before pasting.

Upvotes: 0

Kevin Kozakewich
Kevin Kozakewich

Reputation: 1

I found the answer to my problem. ((Excel.Range)worksheet.Cells[x, y]).Text, where x & y have to be greater than 0. (Starting from 1.)

Upvotes: 0

D.L.MAN
D.L.MAN

Reputation: 1093

Excel Range index is None-Zero and start from 1.
If you call sheet.Rows[0] or sheet.Columns[0] or sheet.Cells[0] exception was thrown, because index is start from 1 and 0 is invalid index.

Upvotes: 2

Chandan Gowda K S
Chandan Gowda K S

Reputation: 1

Hi was Facing the same issue

Steps

  1. make sure you are accessing the excel file, not in read-only mode(protected mode) while trying edit and save changes.

  2. If it happens after that as well you can go to excel file-->Options-->Formulas and here disable the error checking(which is cause for almost most of the HRESULT errors)

Upvotes: 0

fcm
fcm

Reputation: 1331

Interesting enough, this error also occurs, at time of opening when the .XLS? file is incorrectly formed or require repairs.

A hard to find error is to many rows on a .xls (old excel) file.

Test it: manually open the affected file with excel desktop .

I use automation to process a few hundred files daily, when this error show up, I notify the owner via mail and save the unprocessed file on a temporary location.

Upvotes: 0

Idan Youval
Idan Youval

Reputation: 1

I ran to a similar error running Excel in VBA, what I've learned is that when I pull data from MSSQL, and then using get_range and .Value2 apply it's out of the range, any value that was of type uniqueidentifier (GUID) resulted in this error. Only when I cast the value to nvarcahr(max) it worked.

Upvotes: 0

aaron
aaron

Reputation: 357

Using Dominic's answer I found the answer to my problem specifically was an invalid DateTiime in the source data before it was applied to the range. Somewhere between the database, .NET and Excel the conversion of the date defaulted down to "1/1/1899 12:00:00 AM". I had to check it and convert it to an empty string and it fixed it for me.

if (objectArray[row, col].ToString() == "1/1/1899 12:00:00 AM")
{
    objectArray[row, col] = string.Empty;
}

This is probably a pretty specific example but hopefully it will save somebody else some time if they are trying to track down a piece of invalid data.

Upvotes: 1

Elmue
Elmue

Reputation: 8138

The meaning of the completely undocumented error 800A03EC (shame on Microsoft!) is something like "OPERATION NOT SUPPORTED".

It may happen

  • when you open a document that has a content created by a newer Excel version, which your current Excel version does not understand.
  • when you save a document to the same path where you have loaded it from (file is already open and locked)

But mostly you will see this error due to severe bugs in Excel.

  • For example Microsoft.Office.Interop.Excel.Picture has a property "Enabled". When you call it you should receive a bool value. But instead you get an error 800A03EC. This is a bug.
  • And there is a very fat bug in Exel 2013 and 2016: When you automate an Excel process and set Application.Visible=true and Application.WindowState = XlWindowState.xlMinimized then you will get hundreds of 800A03EC errors from different functions (like Range.Merge(), CheckBox.Text, Shape.TopLeftCell, Shape.Locked and many more). This bug does not exist in Excel 2007 and 2010.

Upvotes: 5

Makatun
Makatun

Reputation: 1017

I got this when I forgot to unprotect workbook or sheet.

Upvotes: 3

ebrts
ebrts

Reputation: 381

I ran into this error because I was attempting to write a string to a cell which started with an "=".

The solution was to put an "'" (apostrophe) before the equals sign, which is a way to tell excel that you're not, in fact, trying to write a formula, and just want to print the equals sign.

Upvotes: 7

Jay
Jay

Reputation: 11

I had this problem when I was trying to use the range.AddComment() function. I was able to solve this by calling range.ClearComment() before adding the comment.

Upvotes: 1

Jeremy Thompson
Jeremy Thompson

Reputation: 65554

I got the error with a space in a Sheet Name:

using (var range = _excelApp.Range["Sheet Name Had Space!$A$1"].WithComCleanup())

I fixed it by putting single quotes around Sheet Names with spaces:

using (var range = _excelApp.Range["'Sheet Name Had Space'!$A$1"].WithComCleanup())

Upvotes: 4

Ole
Ole

Reputation:

I found a possible solution here: http://www.made4dotnet.com/Default.aspx?tabid=141&aid=15

Edit:

If you automate Microsoft Excel with Microsoft Visual Basic .NET, Microsoft Visual C# .NET, or Microsoft Visual C++, you may receive the following errors when calling certain methods because the machine has the locale set to something other than US English (locale ID or LCID 1033):

Exception from HRESULT: 0x800A03EC

and/or

Old format or invalid type library

SOLUTION 1:


To get around this error you can set CurrentCulture to en-US when executing code related to Excel and reset back to your originale by using these 2 functions.

//declare a variable to hold the CurrentCulture
System.Globalization.CultureInfo oldCI;
//get the old CurrenCulture and set the new, en-US
void SetNewCurrentCulture()
{
  oldCI = System.Threading.Thread.CurrentThread.CurrentCulture;
  System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
}
//reset Current Culture back to the originale
void ResetCurrentCulture()
{
  System.Threading.Thread.CurrentThread.CurrentCulture = oldCI;
}

SOLUTION 2:


Another solution that could work, create a 1033 directory under Microsoft Office\Office11 (or your corresponding office-version), copy excel.exe to the 1033 directory, and rename it to xllex.dll.

Although you might solve the problem using one off these solutions, when you call the Excel object model in locales other than US English, the Excel object model can act differently and your code can fail in ways you may not have thought of. For example, you might have code that sets the value of a range to a date:

yourRange.Value2 = "10/10/09"

Depending on the locale this code can act differently resulting in Excel putting into the range any of the following values:

October 10, 2009 September 10, 2009 October 9, 2010

Upvotes: 5

Ehsan
Ehsan

Reputation: 4464

If you can copy the whole exception it would be much more better, but once I faced with this Exception and this is because the function calling from your dll file which I guess is Aspose.dll hasn't been signed well. I think it would be the possible duplicate of this

FYI, in order to find out if your dll hasn't been signed well you should right-click on that and go to the signiture and it will tell you if it has been electronically signed well or not.

Upvotes: -2

Dominic Zukiewicz
Dominic Zukiewicz

Reputation: 8444

The error code 0x800A03EC (or -2146827284) means NAME_NOT_FOUND; in other words, you've asked for something, and Excel can't find it.

This is a generic code, which can apply to lots of things it can't find e.g. using properties which aren't valid at that time like PivotItem.SourceNameStandard throws this when a PivotItem doesn't have a filter applied. Worksheets["BLAHBLAH"] throws this, when the sheet doesn't exist etc. In general, you are asking for something with a specific name and it doesn't exist. As for why, that will taking some digging on your part.

Check your sheet definitely does have the Range you are asking for, or that the .CellName is definitely giving back the name of the range you are asking for.

Upvotes: 31

Jay
Jay

Reputation: 10460

I have encountered this error code when enumerating names and calling worksheet.get_Range(name). It seems to occur when the name does NOT apply to a range, in my case it is the name of a macro.

Upvotes: 0

Related Questions