.net excel automation

karynch
karynch used Ask the Experts™
on
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





Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2012
Top Expert 2014

Commented:
Did you download, install and reference the Office PIA?
Most Valuable Expert 2012
Top Expert 2014

Commented:
NorieAnalyst Assistant

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

dim wb  as Excel.Workbook =app.Workbooks.open("c:\a.xls")
Why Diversity in Tech Matters

Kesha Williams, certified professional and software developer, explores the imbalance of diversity in the world of technology -- especially when it comes to hiring women. She showcases ways she's making a difference through the Colors of STEM program.

Author

Commented:
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.)









Most Valuable Expert 2012
Top Expert 2014

Commented:
Remove the old reference and add it again from .NET tab.

Author

Commented:

i did it as you say, result is same,
i sent the image of assembly.
 referenced assembly
Commented:
after installation of  pro office  plus 2010, problem 's gone.
thank you

Author

Commented:
error disappeared

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial