Error 0x800A03EC when creating an excel data sheet - c#

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.

Related

Get current cell value from open Excel sheet using C#

I have an open instance of Excel which is already connected to via a COM process, which I have changed the value of one of the cells. In C# I can't get a connection to the sheet, but in python i can
Working Python code:
import xlwings as xw
wb = xw.Book('c:/users/me/Desktop/mysheet.xlsx')
sheet = wb.sheets['Sheet1']
print(sheet.range('B1').value) # returns the current value
Non-working C# code:
var xlApp = (Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
Workbook wb = xlApp.ActiveWorkbook; // returns null
Worksheet excelSheet = wb.ActiveSheet;
string test = excelSheet.Cells[1, 2].Value.ToString();
Is there a way to get C# to connect to this sheet, given it is already open via a COM automation from another software product?
Most of the online discussion around GetActiveObject is to talk to a specific instance of Excel if there are multiple instances running, I have the opposite problem, a single instance of Excel and multiple applications trying to connect to it.
Workbook wb = System.Runtime.InteropServices.Marshal.BindToMoniker(#"c:\users\me\Desktop\mysheet.xlsx") as Workbook;
should get the workbook even if it is already opened in Excel or not.
https://blogs.msdn.microsoft.com/eric_carter/2009/03/12/attaching-to-an-already-running-office-application-from-your-application-using-getactiveobject-or-bindtomoniker/

Using the COM for opening Excel, can't select a particular sheet

I am using the COM to work with Excel 2007. When I use the following code, it opens to the first sheet by default.
Excel = Sys.OleObject("Excel.Application");
Delay (3000); // Wait until Excel starts
Excel.Visible = true;
Excel.Workbooks.Open("G:\\Documentation\\CalCit Excel Files\\2004 Test Data v3 FINAL_new.xlsx");
I need to select different sheets. I tryed using the following code from the DDTdriver code.
Excel = Sys.OleObject("Excel.Application");
Delay (3000); // Wait until Excel starts
Excel.Visible = true;
Excel.Workbooks.Open("G:\\Documentation\\CalCit Excel Files\\2004 Test Data v3 FINAL_new.xlsx", "sheet2", true);
But this does not work. I have tryed many other configs and still get nothing.
For Excel version 2013 or later :
According to learn.microsoft.com
this can be done with Sheets.Select method :
((Excel.Worksheet)this.Application.ActiveWorkbook.Sheets[1]).Select();
which in the code the selected sheet is 1
another method for older versions of Excel :
Excel.Worksheet sheet = (Excel.Worksheet)this.Sheets["Sheet2"];
sheet.Select(Type.Missing);
where Sheet2 is the name of the sheet

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)";

Open Excel workbook in background using interop

I'm wanting to import data from an Excel workbook without actually displaying the open workbook.
I could have sworn I had used the following code on a previous project and it had worked:
var excelApp = new Excel.Application { Visible = false };
var workbook = excelApp.Workbooks.Open(filePath);
Unfortunately when the workbook opens it is displayed to the user which is unnecessary for this application.
I'm using Microsoft Excel 15.0 Object Library on this project when previously I think it was version 12 or 13. Maybe this is the problem, or is my memory fading and the code is incorrect?
I know this is old but just in case anybody still needs this answer...
excelApp.Visible = false;
(excelApp being the name of the variable used for the excel application)

Excel Interop Exception (2007 installed, Office.Interop.Excel used - ver 1.6 (12)) - 0x800A03EC HResult

Trying to make an excel file using this code:
app = new Excel.Application();
app.Visible = false;
object misValue = System.Reflection.Missing.Value;
workbook = app.Workbooks.Add(misValue);
worksheet = (Excel.Worksheet)workbook.Worksheets.get_Item(1);
and all get if I'm trying to do anything (worksheet.Cells[0,0] = "text")
results in {"Exception from HRESULT: 0x800A03EC"}
I've tried changing the locale:
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
and I've tried also setting "Trust access to the VBA project object model" to true.
Nothing helps.
any ideas ?
Does this happen when you write to any other cell?
This is based off 2003, but I don't think you can write into cell 0,0. The start of the cell is 1,1 which is (A,1)

Categories

Resources