W
Walter
Hi,
I've been having a real hard time converting some VB.NET Excel Automation
code (that works) to C#. I get the app running and can open an existing
spreadsheet but then when trying to get the Workbook reference (and then
hopefully the sheet) I get this error:
Unknown name. (Exception from HRESULT: 0x80020006 (DISP_E_UNKNOWNNAME))
I have tried may methods to get the Workbook reference such as Item,
get_Item, and Open. They all fail. VB uses the Item method (Book =
Books.Item(1) ) to do this so I thought C# would too, but it fails.
Below I'll show the my code then I'll follow it with my C# attempt so far:
VB:
Dim Excel As Object
Dim Books As Object 'Excel.Workbooks
Dim Book As Object 'Excel.Workbook
Dim Sheets As Object 'Excel.Sheets
Dim Sheet As Object 'Excel.Worksheet
Excel = CreateObject("Excel.Application")
Excel.Visible = False
Books = Excel.Workbooks 'get Books reference
Books.Open("c:\myfile.xls")
Book = Books.Item(1) 'get Book reference
Sheets = Book.Worksheets 'get Sheets reference
Sheet = Sheets.item("Monthly Cover") 'get Sheet reference
Excel.Application.Goto(Reference:="MCBillingAccount")
Excel.ActiveCell.FormulaR1C1 = "some data"
Here is C#:
Type Excel;
object excelObject;
object Books;
object Book;
object Sheets;
object Sheet;
object[] Parameters;
Excel = Type.GetTypeFromProgID("Excel.Application");
excelObject = Activator.CreateInstance(Excel);
//get Books reference
Books = excelObject.GetType().InvokeMember("Workbooks",
BindingFlags.GetProperty, null, excelObject, null);
//Open existing spreadsheet
Parameters = new Object[1];
Parameters[0] = pathAndFileName;
Books = excelObject.GetType().InvokeMember("Open",
BindingFlags.InvokeMethod, null, Books, Parameters);
//get the workbook.
Parameters = new Object[1];
Parameters[0] = 1;
//THIS NEXT COMMAND BELOW FAILS
Book = Books.GetType().InvokeMember("Item",
BindingFlags.GetProperty, null, Books, Parameters);
//The code below here has not been reached yet, so I am not sure if it will
work either.
//Get the worksheets collection.
Sheets = Book.GetType().InvokeMember("Worksheets",
BindingFlags.GetProperty, null, Book, null);
//Get the first worksheet.
Parameters = new Object[1];
Parameters[0] = "Monthly Cover";
Sheet = Sheets.GetType().InvokeMember("Item",
BindingFlags.GetProperty, null, Sheets, Parameters);
//Move to Named Reference Cell
Parameters = new Object[1];
Parameters[0] = "MCBillingAccount";
excelObject.GetType().InvokeMember("Goto",
BindingFlags.InvokeMethod, null, Books, Parameters);
Basically, what my app requires is simple:
1. Open existing spreadsheet that has multiple sheets within.
2. Switch to different sheets and insert data into cells, sometimes going to
cell via Named Reference.
Any ideas? Also on how to get to a cell with a Named Reference?
Thanks
Walter
I've been having a real hard time converting some VB.NET Excel Automation
code (that works) to C#. I get the app running and can open an existing
spreadsheet but then when trying to get the Workbook reference (and then
hopefully the sheet) I get this error:
Unknown name. (Exception from HRESULT: 0x80020006 (DISP_E_UNKNOWNNAME))
I have tried may methods to get the Workbook reference such as Item,
get_Item, and Open. They all fail. VB uses the Item method (Book =
Books.Item(1) ) to do this so I thought C# would too, but it fails.
Below I'll show the my code then I'll follow it with my C# attempt so far:
VB:
Dim Excel As Object
Dim Books As Object 'Excel.Workbooks
Dim Book As Object 'Excel.Workbook
Dim Sheets As Object 'Excel.Sheets
Dim Sheet As Object 'Excel.Worksheet
Excel = CreateObject("Excel.Application")
Excel.Visible = False
Books = Excel.Workbooks 'get Books reference
Books.Open("c:\myfile.xls")
Book = Books.Item(1) 'get Book reference
Sheets = Book.Worksheets 'get Sheets reference
Sheet = Sheets.item("Monthly Cover") 'get Sheet reference
Excel.Application.Goto(Reference:="MCBillingAccount")
Excel.ActiveCell.FormulaR1C1 = "some data"
Here is C#:
Type Excel;
object excelObject;
object Books;
object Book;
object Sheets;
object Sheet;
object[] Parameters;
Excel = Type.GetTypeFromProgID("Excel.Application");
excelObject = Activator.CreateInstance(Excel);
//get Books reference
Books = excelObject.GetType().InvokeMember("Workbooks",
BindingFlags.GetProperty, null, excelObject, null);
//Open existing spreadsheet
Parameters = new Object[1];
Parameters[0] = pathAndFileName;
Books = excelObject.GetType().InvokeMember("Open",
BindingFlags.InvokeMethod, null, Books, Parameters);
//get the workbook.
Parameters = new Object[1];
Parameters[0] = 1;
//THIS NEXT COMMAND BELOW FAILS
Book = Books.GetType().InvokeMember("Item",
BindingFlags.GetProperty, null, Books, Parameters);
//The code below here has not been reached yet, so I am not sure if it will
work either.
//Get the worksheets collection.
Sheets = Book.GetType().InvokeMember("Worksheets",
BindingFlags.GetProperty, null, Book, null);
//Get the first worksheet.
Parameters = new Object[1];
Parameters[0] = "Monthly Cover";
Sheet = Sheets.GetType().InvokeMember("Item",
BindingFlags.GetProperty, null, Sheets, Parameters);
//Move to Named Reference Cell
Parameters = new Object[1];
Parameters[0] = "MCBillingAccount";
excelObject.GetType().InvokeMember("Goto",
BindingFlags.InvokeMethod, null, Books, Parameters);
Basically, what my app requires is simple:
1. Open existing spreadsheet that has multiple sheets within.
2. Switch to different sheets and insert data into cells, sometimes going to
cell via Named Reference.
Any ideas? Also on how to get to a cell with a Named Reference?
Thanks
Walter