Trying to manage Excel with vb.net but i recived "Cannot convert COM object from type..."

0

I'm using the "Microsoft.Office.Interop" library to create an excel file. Have office 365 and imported this dll to my vb proyect: "C:\Program Files (x86)\Microsoft Visual Studio 10.0\Visual Studio Tools for Office\PIA\Office12\Microsoft.Office.Interop.Excel.dll"

So, i had this error:

Cannot convert COM object from type 'Microsoft.Office.Interop.Excel.ApplicationClass' to interface type 'Microsoft.Office.Interop.Excel._Application'. An operation error occurred because the QueryInterface call on the COM component for the interface with IID '{000208D5-0000-0000-C000-000000000046}' generated the following error: Item not found. (Exception from HRESULT: 0x8002802B (TYPE_E_ELEMENTNOTFOUND)).

This is the code where i use the library:

Imports Microsoft.Office.Interop

Public Class Fexcel
    Public Sub importarExcel()
        Dim app As Excel.Application
        Dim libro As Excel._Workbook
        Dim hoja As Excel._Worksheet

        app = New Excel.Application
        libro = app.Workbooks.Add
        hoja = app.Worksheets(1)
        Dim direccion As String
    End Sub
End Class

The error is "caused" for this line: libro = app.Workbooks.Add

I quited the reference to the "Office12\Microsoft.Office.Interop.Excel.dll" and added this: ""C:\Program Files (x86)\Microsoft Visual Studio 10.0\Visual Studio Tools for Office\PIA\Office14\Microsoft.Office.Interop.Excel.dll"" (The change is that is the office14 folder) and It didn't work.

I made a new proyect and put the same code and added the reference to "Office14\Microsoft.Office.Interop.Excel.dll" and... It worked. Here's the code of this new proyect:

Imports Microsoft.Office.Interop
Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim app As Excel.Application
        Dim libro As Excel._Workbook
        Dim hoja As Excel._Worksheet

        app = New Excel.Application
        libro = app.Workbooks.Add
        hoja = app.Worksheets(1)
        Dim direccion As String
    End Sub
End Class

I still need to fix the error of my main proyect and i don't know why it happens.

I looked for the error and a something they said to do is look for the registry of the error code to see if you have another version of office installed, i did not have other version of office. (I don't wanna reinstall msOffice, because i don't think it'll work, tell me if i'm wrong)

I think this error is caused in my main proyect; in importations, i don't think it's a thing of visual studio 2010 or office or the registry, but if you think you have a solution, tell it to me, please.


Edit: I've actived the strict mode for my proyect (and had 243 errors). Now i have initialized the Excel objects how they should.

`
Dim app As Excel.Application
Dim libro As Excel.Worksheet
Dim hojaCompr As Excel.Worksheet
Dim hojaVent As Excel.Worksheet
Dim hojaGen As Excel.Worksheet
app = New Excel.Application

Dim direccion As String

libro = CType(app.Workbooks.Add, Excel.Workbook)

The error was caused for trying to define an workbook object without a CType() function to convert from the type Excel.applicationClass to a Excel.Workbook type.

Thank you for helping!!

excel
vb.net
asked on Stack Overflow Nov 27, 2020 by Ricardo A. • edited Nov 28, 2020 by Ricardo A.

1 Answer

0

I fixed it. I defined the objects i was going to use as "Object", changed "app = New Excel.Application" and that's it.

Imports Microsoft.Office.Interop

Dim app As Object
Dim libro As Object
Dim hoja As Object

'Start a new workbook in Excel    
app = CreateObject("Excel.Application")
libro = app.Workbooks.Add

hoja = libro.Worksheets(1)

libro.SaveAs("C:\Users\Ricardo\Documents\book.xlsx")' I will use an getSaveFilename, not this
app.Quit()

Thank you for helping

answered on Stack Overflow Nov 27, 2020 by Ricardo A.

User contributions licensed under CC BY-SA 3.0