khalamuntu
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. Applicatio n"), 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.E xcel.Workb ook
Dim xlSheet As Microsoft.Office.Interop.E xcel.Works heet
xlApp = CType(CreateObject("Excel. Applicatio n"), Microsoft.Office.Interop.E xcel.Appli cation)
xlBook = CType(xlApp.Workbooks.Add, Microsoft.Office.Interop.E xcel.Workb ook)
xlSheet = CType(xlBook.Worksheets(1) , Microsoft.Office.Interop.E xcel.Works heet)
---
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
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.
xlBook = CType(xlApp.Workbooks.Add,
xlSheet = CType(xlBook.Worksheets(1)
---
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.E
Dim xlSheet As Microsoft.Office.Interop.E
xlApp = CType(CreateObject("Excel.
xlBook = CType(xlApp.Workbooks.Add,
xlSheet = CType(xlBook.Worksheets(1)
---
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
ASKER
Yes and its version 11.0
source:'c:\program files\microsoft office\0ffice11\excel.exe'
!k*
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.Applic ation")
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:\
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
xlApp = CreateObject("Excel.Applic
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:\
ASKER
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*
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 ?
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.
I have just tried RonaldBiemans example and worked fine for me too.
ASKER
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 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.
Should it not say:
"Excel.Application"
Desperately seeking help is,
!k*
ASKER
I have changed all instances of "Excel." to "Microsoft.Office.Interop. Excel."
---
Dim xlApp As Microsoft.Office.Interop.E xcel.Appli cation
Dim xlBook As Microsoft.Office.Interop.E xcel.Workb ook
Dim xlSheet As Microsoft.Office.Interop.E xcel.Works heet
xlApp = CreateObject("Excel.Applic ation")
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.MissingMemberExcep tion' occurred in microsoft.visualbasic.dll
Additional information: Public member 'Workbook' on type 'ApplicationClass' not found.
---
---
Dim xlApp As Microsoft.Office.Interop.E
Dim xlBook As Microsoft.Office.Interop.E
Dim xlSheet As Microsoft.Office.Interop.E
xlApp = CreateObject("Excel.Applic
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.MissingMemberExcep
Additional information: Public member 'Workbook' on type 'ApplicationClass' not found.
---
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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!
Yes, I found that the " .NET Programmability Support" was not installed. I will get on it and come back with feedback tomorrow.
Thanks,
*k!
ASKER
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*
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
Donnie
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)