karynch
asked on
.net excel automation
My goal is - in a .net project(vs2005, vb.net, [windows form application] project,office2007 or office 2003)- to open an excel file , write in it and close it .
In the project i referenced to com assembly which is [ excel object library 11.0]
here is the simple code i am trying to run.
dim app as Excel.Application = new Excel.Application
dim wb as Workbook =app.Workbooks.open("c:\a. xls")
dim sheet as Excel.Worksheet =wb.Sheets("Sheet1")
dim range as Excel.Range= sheet.Range("A1")
when i execute this code,at the last line of the code, crushes with exception:
Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop. Excel.Rang e'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{00020846-0000-0000-C000- 0000000000 46}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
Could you help me about it, thanks already
In the project i referenced to com assembly which is [ excel object library 11.0]
here is the simple code i am trying to run.
dim app as Excel.Application = new Excel.Application
dim wb as Workbook =app.Workbooks.open("c:\a.
dim sheet as Excel.Worksheet =wb.Sheets("Sheet1")
dim range as Excel.Range= sheet.Range("A1")
when i execute this code,at the last line of the code, crushes with exception:
Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.
Could you help me about it, thanks already
Did you download, install and reference the Office PIA?
Works fine for me if I add Excel in front of Workbook.
dim wb as Excel.Workbook =app.Workbooks.open("c:\a. xls")
dim wb as Excel.Workbook =app.Workbooks.open("c:\a.
ASKER
i installed pia, i didnot changed referenced assemblies( please tell me if i have to do something different to reference these freshly installed PIA ); so it is still COM assembly which is excel object library 11.
well, it did not changed situation, still getting same error.
imnorie,
up there i forgot add Excel namespace, but in the actual code, it is like Excel.Workbook.
one more thing:
if i do something like :
wb.Sheets("Sheet1").Range( "A1")="Hel lo"
it does the job-getting no error;
but while coding, when you put period after
wb.Sheets("Sheet1")
intellisense does not bring the members of Range.
( compiler does the cast operation dynamically i think.)
well, it did not changed situation, still getting same error.
imnorie,
up there i forgot add Excel namespace, but in the actual code, it is like Excel.Workbook.
one more thing:
if i do something like :
wb.Sheets("Sheet1").Range(
it does the job-getting no error;
but while coding, when you put period after
wb.Sheets("Sheet1")
intellisense does not bring the members of Range.
( compiler does the cast operation dynamically i think.)
Remove the old reference and add it again from .NET tab.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
error disappeared