Hello i'm trying to import data from an excel to visual basic variables, but im getting an strange error. I did add the reference to microsoft excel com library.
Imports Microsoft.Office.Interop.Excel
Module Module1
Sub Main()
ExtraerCostos()
End Sub
Public Sub ExtraerCostos()
Dim numero As String
Dim aux As String
Dim costos(20) As Double
Dim cant As Integer
Dim excelApp As New Microsoft.Office.Interop.Excel.Application
Dim workbook As New Microsoft.Office.Interop.Excel.Workbook ' The error points to this line
Dim sheet As New Microsoft.Office.Interop.Excel.Worksheet
excelApp = CreateObject("Excel.Application")
excelApp.Visible = True
workbook = excelApp.Workbooks.Open("C:\workbook.xls")
sheet = workbook.Worksheets("Factura Detallada")
'Irrelevant code
numero = "111111111"
cant = 12
While numero.Length = 9
cant = cant + 1
End While
For i = 12 To cant
numero = sheet.Cells(i, 1).text
For j = 3 To 22
aux = sheet.Cells(i, j).text
If aux = "-" Then
costos(j - 2) = 0
Else : costos(j - 2) = Convert.ToDouble(aux)
End If
Console.WriteLine(costos(j - 2))
Next
Next
End Sub
End Module
Retrieving the COM class factory for component with CLSID {00020819-0000-0000-C000-000000000046} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).
Best Answer
you can't create a workbook or worksheet in this way. you have to use the Workbooks.Open or Workbooks.Add method (you do that in line 10 of ExtraerCostos)
use
Dim workbook as Excel.Workbook
and already was okay