Visual Basic - Open an Excel file

2

I want to manipulate a Excel file with Visual Basic , using Microsoft Excel 16.0 Object Library for this I have:

Imports Excel = Microsoft.Office.Interop.Excel
Public Class Form1
    Public Aplicacion As New Excel.Application
    Public Libro As New Excel.Workbook
    Public Hoja As New Excel.Worksheet


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Libro = Aplicacion.Workbooks.Open("test.xlsx")
        Hoja = Aplicacion.Worksheets("Hoja1")
    End Sub
End Class

But the answer is an exception:

  

Error creating the form. See Exception.InnerException for more details. Error: The COM class generator for the component with CLSID {00020819-0000-0000-C000-000000000046} could not be recovered due to the following error: 80040154 Class not registered (HRESULT exception: 0x80040154 (REGDB_E_CLASSNOTREG)).

I have Office 2016 installed

How can I fix the error?

In Response to Cristina

It's Windows Forms project. As for assigning permissions, I do not understand what you mean, and configure user in DCOM.

Tell me how I can do what you mention.

    
asked by Máxima Alekz 24.12.2016 в 18:20
source

2 answers

2

I found the answer , here I leave the code complete:)

In the code, a button is used to open the file.

When the actions are finished, the application is closed.

Imports Excel = Microsoft.Office.Interop.Excel
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click

        Dim Aplicacion As Excel.Application
        Dim Libro As Excel.Workbook
        Dim Hoja As Excel.Worksheet

        Aplicacion = New Excel.Application
        Libro = Aplicacion.Workbooks.Open("C:\Test.xlsx")
        Hoja = Libro.Worksheets("SEMANA1")

        'Aquí manipulen su archivo
        'Aquí manipulen su archivo
        'Aquí manipulen su archivo

        Libro.Close()
        Aplicacion.Quit()

        releaseObject(Aplicacion)
        releaseObject(Libro)
        releaseObject(Hoja)

    End Sub

    Private Sub releaseObject(ByVal obj As Object)
        Try
            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
            obj = Nothing
        Catch ex As Exception
            obj = Nothing
        Finally
            GC.Collect()
        End Try
    End Sub
End Class
    
answered by 27.12.2016 / 15:14
source
1

Works perfectly thanks, a contribution if required:

Book.save () Book.Close () Application.Quit ()

releaseObject (Application) releaseObject (Book) releaseObject (Sheet)

    
answered by 17.04.2018 в 18:07