Click here to Skip to main content
15,918,742 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Imports Excel = Microsoft.Office.Interop.Excel
Imports Microsoft.Office.Interop
Imports Microsoft.Office.Interop.Excel

Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

        Select Case ComboBox1.SelectedItem

            Case "Open Excel"

                Dim oExcel As New Microsoft.Office.Interop.Excel.Application()
                Dim oBook As Excel.WorkbookClass
                Dim oBooks As Excel.WorkbookClass
                Dim Open As New Excel.ApplicationClass

                'Start Excel and open the workbook
                oExcel.Visible = True
                'oBooks = oExcel.Workbooks

                oBook = oExcel.Workbooks.Open("H:\ExportExcelBook.xlsm")

Good day! I have an error of InvalidCastException was unhandled at
oBook = oExcel.Workbooks.Open("H:\ExportExcelBook.xlsm")
I'm able to open up that workbook but the program gave me this issue after that. I'm using Microsoft Visual Studio to run this program. May I know how to solve this problem?

What I have tried:

Adding references but I'm not sure which reference is missing in this case.
Updated 6-Mar-18 21:58pm

oBook is declared as a WorkBookClass[^]. If you go to that documentation you will see the very first line says
Reserved for internal use.
Use a Workbook - which is what Workbooks.Open[^] returns
Share this answer
CPallini 7-Mar-18 3:36am    
Maciej Los 7-Mar-18 3:59am    
Agree, a 5!
Please, see my answer.
I already answered your question here: Error of missingmemberexception was unhandled[^]
And i did provide 2 links to MSDN documentation you have to visit. Don't you?
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900