Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to call Excell to VB. keep on giving a error when getting the excel file. Is it because i'm calling the Excel 2007 application. when I calling Excel 2003 it works


VB
Dim xlApp As New Excel.Application
        Dim xcFileInfo As IO.FileInfo
        Dim xcFileDialog As New OpenFileDialog()
        Dim strFileName As String
        xcFileDialog.Filter = "Excel Spreadsheet Files!|*.xls"
        xcFileDialog.Title = "Select estimate in excel spreadsheet file!"
        If xcFileDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
            xcFileInfo = New IO.FileInfo(xcFileDialog.FileName)
            xlApp = CreateObject("Excel.Application")
            strFileName = xcFileDialog.FileName
            'MessageBox.Show(strFileName)
            xlApp.Workbooks.Open(strFileName)
        End If
        xlApp.Visible = True
    End Sub
Posted
Updated 24-Aug-11 18:40pm
v2
Comments
ChandraRam 24-Aug-11 10:55am    
Why are you calling CreateObject when the object type is already known? In other words, why use late binding, when you have already typed the variable? And, nowhere in this snippet is the code bound to a version of Excel (except in the dialog filter, when you are not allowing .xlsx files).
Onke Nkwentsha 25-Aug-11 4:05am    
I was thought that way. I will try what you suggest and see how it goes. Thanks!

1 solution

Why you are use CreateObject("Excel.Application"). you have already declear Dim xlApp As New Excel.Application excel application object.

code to open excel file same code will run for 2003/2007
VB
Dim objExcel As Microsoft.Office.Interop.Excel.Application
Dim objWB As Microsoft.Office.Interop.Excel.Workbook
Try
   objExcel = New Microsoft.Office.Interop.Excel.Application
   objWB = excel.Workbooks.Open("c:\\test.xls")
   objExcel.Visible = True
   objWB.Activate()
Catch ex As COMException
   MessageBox.Show("Error accessing Excel: " + ex.ToString())
End Try
 
Share this answer
 
v2

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