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
LVL 1
khalamuntuAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RonaldBiemansCommented:
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)
0
khalamuntuAuthor Commented:
Yes and its version 11.0
source:'c:\program files\microsoft office\0ffice11\excel.exe'


!k*
0
RonaldBiemansCommented:
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:\
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

khalamuntuAuthor Commented:
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*
0
RonaldBiemansCommented:
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 ?
0
cmellonCommented:
I would try removing the reference and adding it again,

I have just tried RonaldBiemans example and worked fine for me too.
0
khalamuntuAuthor Commented:
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*
0
khalamuntuAuthor Commented:
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.
---

0
cmellonCommented:
Hi Khalamuntu,

Microsoft.Office.Interop.Excel is correct,

All i did when i tried it is as you explained above, added the reference and ran the same code as you with no problems.

I have just created a new project added the Microsoft Excell reference and i have a couple of references which you don't seem to have.

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

I notice you haven't got VBIDE, or stdole listed in yours.  I didn't add these myself, they were automatically added when i reference excel.

Try adding them manually, the stdole one can be added from the .net tab in add reference, and the VBIDE one can be added form the COM tab by selecting Microsoft Visual Basic for Applications Extensibility 5.3.

Let me know if this has fixed it or not.

If not i would try removing Office and Re-Installing it.

Regards

Craig
0
Donnie4572Commented:
Sounds like the Office 2003 Interop assemblies are not available for your program. Even though you made the correct reference to office, the .dll's may not be available.

Try This.....

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_wrcore/html/wrtskinstallingofficeprimaryinteropassemblies.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_wrcore/html/wrrefofficeprimaryinteropassemblies.asp

Donnie

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
khalamuntuAuthor Commented:
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!
0
khalamuntuAuthor Commented:
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*
0
Donnie4572Commented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.