Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ComException when adding an Workbook in Excel

Status
Not open for further replies.

NorAceMan

Programmer
Jun 22, 2005
5
0
0
NO
Hello.

I get an 'System.Runtime.InteropServices.COMException' with Additional information: 'Old format or invalid type library.' when adding a workbook in an instance of Excel from VB.Net.

This is the latest code I've tried:

Dim xlsApp As Excel.Application

xlsApp = CType(CreateObject("Excel.Application.9"), Excel.Application)

Dim wbNew As Excel.Workbook = xlsApp.Workbooks.Add("c:\bok1.xlt")

I've also tried CreateObject etc, but I allways get the same Error message.
 
is the object library in your project the correct version - Version 9(Office 2000) as you've stated in the CreateObject command...

Code:
xlsApp = CType(CreateObject("Excel.Application.9"), Excel.Application)

you actually don't even need the createobject command if you've got excel in your references

Code:
dim xlsApp as NEW excel.application
Dim wbNew As Excel.Workbook = xlsApp.Workbooks.Add("c:\bok1.xlt")

if "c:\bok1.xlt" can't be found it will probably cause a COM error also

 
Thanks!

I have now change my code to this:

Code:
Dim xlsApp As New Excel.Application
Dim wbNew As Excel.Workbook = xlsApp.Workbooks.Add()

Still I get the same error on the second line.
 
this works for me on 2003, give me about an hour and I'll have access to an Office 2000 machine.

In the meantime, remove the reference that you have to the Excel object library (dll) and add it in again - it might just need refreshing due to uninstall/project movement etc...

Also, delete the Excel interop in your bin directory (or wherever it is), the project will recreate it on next build.

 
Firstly,the interop will be in the 'obj' directory not the 'bin' directory as I previously stated.

I've just checked an Office 2000 machine and your code works...

EXCEPT...

As a long shot try removing the brackets or enter a file name in the following line...
Code:
Dim wbNew As Excel.Workbook = xlsApp.Workbooks.Add()
Because it's a COM object it might not be handling the empty brackets very well.

If that doesn't fix it then it's some sort of version issue which could be related to the Excel dll or the interop Excel dll.

Go to the Excel object in project references and look under the Description property. It should read something like 'Microsoft Excel 9.0 Object Library'. Also make a note of the Path property which will be the interop file and delete that interop file (Make sure it's not a dll in Microsoft Office directories first! - it shouldn't be but with this sort of screwy issue you can't be certain)

After verifying the above your best bet would be to create a new test project, add the Excel object library to the references and then see if your code works in this new environment.

If it does, your original project has a glitch (dll/interop).
If it doesn't then there's something wrong with your Office installation (or your machine). Maybe also your VS.Net installation but I doubt it.

 
I've now tried what the above without success. I'll try to upgrade to Excel 2003, and see if that may solve the problem!
 
I have WinXp and Office 2000, and I got the same error when trying to open a workbook based on a template. The following did work however
Code:
Dim xlsApp As New Excel.Application
Dim wbnew As Excel.Workbook = xlsApp.Workbooks.Add()
wbnew.SaveAs("c:\test1.xls")
wbnew.Close()
xlsApp = Nothing
When the COM operation fails, it does leave a copy of Excel laying around, and you need to use the Task Manager to clear these objects.




Sweep
...if it works dont f*** with it
...if its f****ed blame someone else
...if its your fault that its f***ed, say and admit nothing.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top