Link to home
Start Free TrialLog in
Avatar of khalamuntu
khalamuntuFlag for South Africa

asked on

Error: Type 'Excel.Application' Is Not Defined

Hi,

I am new to .NET and I am doing exercises from tutorials.  I am now trying to automate excel but I come up with the above error.  This is my code:-

---
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
xlApp = CType(CreateObject("Excel.Application"), Excel.Application)
xlBook = CType(xlApp.Workbooks.Add, Excel.Workbook)
xlSheet = CType(xlBook.Worksheets(1), Excel.Worksheet)
---

I get an error the error on each line where there is "Excel.",  I have read somewhere that my code should look something like this:-

---
Dim xlBook As New Microsoft.Office.Interop.Excel.Workbook
 Dim xlSheet As Microsoft.Office.Interop.Excel.Worksheet
xlApp = CType(CreateObject("Excel.Application"), Microsoft.Office.Interop.Excel.Application)
xlBook = CType(xlApp.Workbooks.Add, Microsoft.Office.Interop.Excel.Workbook)
xlSheet = CType(xlBook.Worksheets(1), Microsoft.Office.Interop.Excel.Worksheet)
---

With this, the form actually loads but does not open excel with an exception error on the last line above.

Please help!


!k*


-------------
ARTILLERY
-------------
Microsoft Windows XP SP2
Microsoft Office 2003
Microsoft Visual Studio 2003
Avatar of RonaldBiemans
RonaldBiemans

Hi khalamantu,

Have you set a refence to the Excel object ?

solution explorer right click references / add references, choose the com tab, then choose the microsoft excel x.x Object library (where x.x is the excel version you have on your computer)
Avatar of khalamuntu

ASKER

Yes and its version 11.0
source:'c:\program files\microsoft office\0ffice11\excel.exe'


!k*
Well if you have referenced Excel like you said then this should be enough

        Dim xlApp As Excel.Application
        Dim xlBook As Excel.Workbook
        Dim xlSheet As Excel.Worksheet
        xlApp = CreateObject("Excel.Application")
        xlBook = xlApp.Workbooks.Add
        xlSheet = xlBook.Worksheets(1)
        xlSheet.Cells.Item(1, 1) = "test"
        xlBook.SaveAs("c:\tttt.xls")
        xlBook.Close()
        xlApp = Nothing

I tested this and it works (this will open a new workbook, put the word 'test' in cell A1 and than saves it as tttt.xls in the c:\
No, it is not working....

On this line below:
Dim xlApp As Excel.Application

I get a warning (ziz-zag underline) on Excel.Application.

RonaldBiemans, the system you tested it on, is it similar to mine? I am thinking that this error has to do with Office 2003.


!k*
Hi khalamantu,

No I have exactly the same setup,
The fact that it underlines excel.application, lets be believe that you haven't referenced the excel object library.
When you look at your references to you see excel and office there ?
I would try removing the reference and adding it again,

I have just tried RonaldBiemans example and worked fine for me too.
cmellon,

I have removed and re-referenced it again.  I can see it on my solution explorer, like this:

- References
     Excel
     Microsoft Office Core
     System
     System.Data
     System.Drawing
     System.Windows.Forms
     System.XML

When I click once Excel on the properties window I get:
"Microsoft.Office.Interop.Excel"

Should it not say:
"Excel.Application"


Desperately seeking help is,
!k*
I have changed all instances of "Excel." to "Microsoft.Office.Interop.Excel."

---
Dim xlApp As Microsoft.Office.Interop.Excel.Application
        Dim xlBook As Microsoft.Office.Interop.Excel.Workbook
        Dim xlSheet As Microsoft.Office.Interop.Excel.Worksheet
        xlApp = CreateObject("Excel.Application")
        xlBook = xlApp.Workbook.Add()
        xlSheet = xlBook.Worksheets(1)
        xlSheet.Cells.Item(1, 1) = "test"
        xlBook.SaveAs("d:\tttt.xls")
        xlBook.Close()
        xlApp = Nothing
---

and now I get this error, when I click the form button:

---
An unhandled exception of type 'System.MissingMemberException' occurred in microsoft.visualbasic.dll
Additional information: Public member 'Workbook' on type 'ApplicationClass' not found.
---

SOLUTION
Avatar of cmellon
cmellon

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
ASKER CERTIFIED SOLUTION
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
Sorry for not responding sooner,  I've been where there isn't internet access.

Yes, I found that the " .NET Programmability Support" was not installed.  I will get on it and come back with feedback tomorrow.

Thanks,

*k!
OK!

It worked but I had to reference "stdole" and "vbide" myself, it didn't autoreference with Excel.  In fact I had to browse for vbide, it was n't even on the COM list.

So I am thinking bad installation here, but is it VS or Office?

Anyway, thanks to all you guys for helping.

Donnie  and Craig, I hope you don't mind sharing the points as both your answers helped.


Thanks a mil!

!k*
I also had to browse to the vbide don't think I used the stdole. You might want to copy those dll's to a folder in your project then reference them from there otherwise when you deploy the project you might see a dependency problem.
Donnie