Link to home
Start Free TrialLog in
Avatar of karynch
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.Range'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{00020846-0000-0000-C000-000000000046}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

Could you help me about it, thanks already





Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Did you download, install and reference the Office PIA?
Avatar of Norie
Norie

Works fine for me if I add Excel in front of Workbook.

dim wb  as Excel.Workbook =app.Workbooks.open("c:\a.xls")
Avatar of karynch

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")="Hello"

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.
Avatar of karynch

ASKER


i did it as you say, result is same,
i sent the image of assembly.
 User generated image
ASKER CERTIFIED SOLUTION
Avatar of karynch
karynch

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of karynch

ASKER

error disappeared