none
error: Class not registered RRS feed

  • Question

  • Hello,

    I'm trying to use a Visual Basic windows form app to open microsoft office 2013 Excel.  When my program tries to get Excel to open I get the following error.

    An exception of type 'System.Runtime.InteropServices.COMException' occurred in mscorlib.dll but was not handled in user code

    Additional information: Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).

    My code is:

    Imports Excel = Microsoft.Office.Interop.Excel

    Public Class Form1

        '~~> Define your Excel Objects
        Dim xlApp As New Excel.Application
        Dim xlWorkBook As Excel.Workbook
        Dim xlSheet As Excel.Worksheet


    Friday, May 26, 2017 9:48 PM

All replies

  • Hi philb20045,

    try to use code below. I tested on my side and find that it is working correctly on my side.

    Imports Excel = Microsoft.Office.Interop.Excel
    Public Class Form1
    
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            OpenExcelDemo("C:\Users\v-padee\Desktop\Book4.xlsx", "Sheet1")
        End Sub
        Public Sub OpenExcelDemo(ByVal FileName As String, ByVal SheetName As String)
            If IO.File.Exists(FileName) Then
                Dim Proceed As Boolean = False
                Dim xlApp As Excel.Application = Nothing
                Dim xlWorkBooks As Excel.Workbooks = Nothing
                Dim xlWorkBook As Excel.Workbook = Nothing
                Dim xlWorkSheet As Excel.Worksheet = Nothing
                Dim xlWorkSheets As Excel.Sheets = Nothing
                Dim xlCells As Excel.Range = Nothing
                xlApp = New Excel.Application
                xlApp.DisplayAlerts = False
                xlWorkBooks = xlApp.Workbooks
                xlWorkBook = xlWorkBooks.Open(FileName)
                xlApp.Visible = True
                xlWorkSheets = xlWorkBook.Sheets
                For x As Integer = 1 To xlWorkSheets.Count
                    xlWorkSheet = CType(xlWorkSheets(x), Excel.Worksheet)
                    If xlWorkSheet.Name = SheetName Then
                        Console.WriteLine(SheetName)
                        Proceed = True
                        Exit For
                    End If
                    Runtime.InteropServices.Marshal.FinalReleaseComObject(xlWorkSheet)
                    xlWorkSheet = Nothing
                Next
                If Proceed Then
                    xlWorkSheet.Activate()
                    MessageBox.Show("File is open, if you close Excel just opened outside of this program we will crash-n-burn.")
                Else
                    MessageBox.Show(SheetName & " not found.")
                End If
                xlWorkBook.Close()
                xlApp.UserControl = True
                xlApp.Quit()
                ReleaseComObject(xlCells)
                ReleaseComObject(xlWorkSheets)
                ReleaseComObject(xlWorkSheet)
                ReleaseComObject(xlWorkBook)
                ReleaseComObject(xlWorkBooks)
                ReleaseComObject(xlApp)
            Else
                MessageBox.Show("'" & FileName & "' not located. Try one of the write examples first.")
            End If
        End Sub
        Public Sub ReleaseComObject(ByVal obj As Object)
            Try
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
                obj = Nothing
            Catch ex As Exception
                obj = Nothing
            End Try
        End Sub
    
    
    
    End Class
    
    

    Reference:

    Open an Excel File using VB.Net

    How to: Programmatically Open Workbooks

    Workbooks.Open Method (Excel)

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, May 29, 2017 5:52 AM
    Moderator