?
Solved

Error: Type 'Excel.Application' Is Not Defined

Posted on 2004-11-28
13
Medium Priority
?
8,691 Views
Last Modified: 2008-01-09
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
0
Comment
Question by:khalamuntu
  • 6
  • 3
  • 2
  • +1
13 Comments
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 12690149
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
 
LVL 1

Author Comment

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


!k*
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 12690240
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 1

Author Comment

by:khalamuntu
ID: 12690389
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
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 12690460
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
 
LVL 1

Expert Comment

by:cmellon
ID: 12690765
I would try removing the reference and adding it again,

I have just tried RonaldBiemans example and worked fine for me too.
0
 
LVL 1

Author Comment

by:khalamuntu
ID: 12698629
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
 
LVL 1

Author Comment

by:khalamuntu
ID: 12699185
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
 
LVL 1

Assisted Solution

by:cmellon
cmellon earned 400 total points
ID: 12699533
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
 
LVL 12

Accepted Solution

by:
Donnie4572 earned 600 total points
ID: 12708785
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
 
LVL 1

Author Comment

by:khalamuntu
ID: 12719366
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
 
LVL 1

Author Comment

by:khalamuntu
ID: 12726309
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
 
LVL 12

Expert Comment

by:Donnie4572
ID: 12728091
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Integration Management Part 2
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month13 days, 15 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question