SpecialCells throws exception HRESULT: 0x800AC472 - c#

I'm writing a code in C# that reads input from Excel file and place it into objects. when I try to get the last row, sometimes I get an exception HRESULT: 0x800AC472.
this is the code:
Excel.Application ExcelObj = new Excel.Application();
ExcelObj.Visible = false;
Excel.Workbook workBook = ExcelObj.Workbooks.Open(Name);
Excel.Worksheet sheet = (Excel.Worksheet)ExcelObj.Sheets[1];
int lastRow = 0;
try {
lastRow=sheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell).Row;
}
I've searched google for a solution but found nothing. How can I solve it?

Take a look at this MSDN question. What he says to do is to create a retry loop around your code. This SO question had a similar problem that this technique seemed to resolve.

Related

HRESULT: 0x800401A8 when getting UsedRange of Worksheet

I'm trying to open an Excel Sheet, get the UsedRange and loop through it.
This already worked once, but then I had the exception
COM object that has been separated from its underlying RCW cannot be
used
after using
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkBook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkSheet);
Note that the exception kept coming up in the code below as well, not when actually releasing. I did this the first time and since then I couldn't get the code below to work. Sadly, I don't know at which line exactly he crashed anymore.
Because the exception kept comming up, I restarted my PC and since then when trying to access UsedRange, I get an Exception:
System.Runtime.InteropServices.COMException: "Ausnahme von HRESULT:
0x800401A8"
Here is the code:
using Excel = Microsoft.Office.Interop.Excel;
....
var xlApp = new Excel.Application();
var xlWorkBook = xlApp.Workbooks.Open(file);
Excel.Worksheet xlWorkSheet = xlWorkBook.Sheets["Tabelle1"];
for (int i = 1; i < xlWorkSheet.UsedRange.Rows.Count; i++)
{ ...
This worked once, so I'm thinking the COM object is still not working correctly. (Like in the first exception)
What can I do to make it work again?
What happened is that I accidentally closed the Workbook and quitted the App in the foor loop and not after it. I accidentally put it in front of the }.
for (int i = 1; i < xlWorkSheet.UsedRange.Rows.Count; i++)
{
...
xlWorkBook.Close(false);
xlApp.Quit();
}
Of course, that's supposed to go after the loop.
You cannot access a variable in a workbook that isn't open anymore.
So if you've got the error, check if the workbook is actually really open.
Shame on me.

Create Excel Sheet with LINEST via C#

I have a C# application which has to create an Excel file with formulas.
Simple formulas work fine.
But when I use LINEST(...) I get an exception.
Init objects:
using Microsoft.Office.Interop.Excel;
var Application xlApp = new Microsoft.Office.Interop.Excel.Application();
Workbook xlWorkBook = xlApp.Workbooks.Add(misValue);
var currentWorksheet = xlWorkBook.Worksheets.get_Item(1);
string myLinestFormulaString ="LINEST(LN(C1:C10);$A1:$A10;TRUE;FALSE)";
The line below causes an exception HRESULT: 0x800A03EC
currentWorksheet.Cells[12, 3].Formula = myLinestFormulaString; // Write to C12
I know the HRESULT means that there is something wrong with the formula string.
Edit: Issue solved.
I use the German version of Excel. In C# I had to replace ';' with commas.
Excel's COM interface use the American list separators. That's why you need , instead of ; in your formular.
Try:
string myLinestFormulaString = "LINEST(LN(C1:C10),$A1:$A10,TRUE,FALSE)";

Error 0x800A03EC when creating an excel data sheet

Iam trying to create an excel file with the info within the datagridview which I get from a db. but I get the following error Exception from HRESULT: 0x800A03EC" (System.Runtime.InteropServices.COMException)... the error seems to be in the line with **
the code is within a button click
Microsoft.Office.Interop.Excel.Application excel1 = new Microsoft.Office.Interop.Excel.Application { };
Workbook work1 = excel1.Workbooks.Add(XlSheetType.xlWorksheet);
Worksheet ws1 = (Worksheet)excel1.ActiveSheet;
excel1.Visible = true;
ws1.Cells[1,1] = "ID";
ws1.Cells[1,2]= "NAME";
** ws1.Cells[1,1]= dataGridView1[1,1];
ws1.Cells[1, 2] = dataGridView1[1,2];
There are references available, for example see:
COM exception diagnosing:
https://msdn.microsoft.com/en-US/library/af1y26ew(v=vs.80).aspx
Depending upon what operating system you are using, an old excel file access bug:
https://social.msdn.microsoft.com/Forums/vstudio/en-US/4d6c383a-94eb-4898-9d22-aa4bb69be25b/excel-interop-systemruntimeinteropservicescomexception-0x800a03ec-microsoft-office-excel?forum=vbgeneral
"When working with excel, it is better to work with the ranges instead than directly with the cells. The reason is that with the cells you are using late binding and you only see the COM errors":
https://social.msdn.microsoft.com/Forums/vstudio/en-US/6a9ba772-f3d8-413e-8243-2846dfdb8b22/probem-with-systemruntimeinteropservicescomexception-0x800a03ec-exception-from-hresult?forum=vbgeneral
When setting cells does not work:
VSTO write to a cell in Excel!
And lastly...
Exception from HRESULT: 0x800A03EC Error
These are but a few to look at.

Excel Interop's set_value method damages values

I found a problem while using the set_value method. I wrote the simplest possible code to exclude my mistakes.
When I read some cells and then write them back to sheet using set_value while:
any filters are active in xlsx file,
2 or more columns are get/set,
some of the cells are corrupted (they are empty or contain junk like #N/D!). When I disable filtering in worksheet or in program before using set_value, everything works fine. I can't find any info about this specific problem in google, am I doing something really stupid?
Excel.Workbooks workbooks;
Excel.Workbook workbook = null;
Excel.Worksheet sheet = null;
Excel.Application excelApplication;
excelApplication = new Excel.Application();
excelApplication.Visible = false;
workbooks = excelApplication.Workbooks;
workbook = workbooks.OpenXML(file);
sheet = workbook.Sheets[1];
Excel.Range excelRangeR = sheet.UsedRange;
object[,] valueArrayR = (object[,])excelRangeR.get_Value(Excel.XlRangeValueDataType.xlRangeValueDefault);
//if uncomment line below (or manualy disable filters in XLSX file), set_value works fine
//sheet.AutoFilterMode = false;
excelRangeR.set_Value(Excel.XlRangeValueDataType.xlRangeValueDefault, valueArrayR);
workbook.Save();
workbook.Close();
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelRangeR);
System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbooks);
excelApplication.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApplication);
You can leave the filters on, but if there is an active filter on the sheet, that has to be removed before setting the value of the cells. I think what is happening is that when you set the object[,] variable, it's populating the array with all the data in the range, including the rows that are not visible, but when you set the value of the range to the object[,] array, it only overwrites the visible rows. It's either that or vice versa.
Sorry if this code is incorrect. I'm just guessing at the C# Interop syntax. I don't use it all that often.
if (sheet.get_FilterMode()) {
sheet.ShowAllData();
}
Unfortunately there isn't an easy way to save the current value of the worksheet filters, so it would be difficult if you need to be able to restore the filters after the paste operation.

C# generating excel file and writing to cells

I am working on a project that runs MySQL queries on DB. I need to feed out the results to a excel sheet that I will be generating on the fly. I had started the code, using msdn tutorials as well as information I found on stackoverflow, however running the application leaves me with
"Object reference not set to an instance of an object."
At this point I just have the basic code started as I wanted to make this work before I started creating the entire spreadsheet in the code.
var excelApp = new Excel.Application();
excelApp.Visible = true;
Excel.Worksheet workSheet = (Excel.Worksheet)excelApp.ActiveSheet;
((Excel.Range)workSheet.Cells[1, 1]).Value = "Print Date:";
If I can provide any more info just ask. I greatly appreciate any information or insight you can provide me with!
I'd say that workSheet is being set to null as excelApp.ActiveSheet doesn't exist yet.
Here's some code that'll create a worksheet object you can use...
var application = new Application();
var workbooks = application.Workbooks;
var workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet);
var worksheets = (Sheets)workbook.Worksheets;
var worksheet = worksheets[1];
A few things to note when working with Excel:
Release every object with System.Runtime.InteropServices.Marshal.ReleaseComObject(object) when you're finished with it. If you don't, Excel won't exit when you're done.
Don't use double-dot references (i.e., object1.object2.value). If you do you can't release it properly as above.
Indexes always start from 1, not 0 (in my experience anyway).
The reason you get that error is, you have not created any worksheets in the Application.
Try doing this.
Excel.Worksheet newWorksheet;
newWorksheet = (Excel.Worksheet)excelApp.Worksheets.Add();

Categories

Resources