Opening an 2003 Excel spreadsheet

  • Thread starter Thread starter BGCSOCAL
  • Start date Start date
B

BGCSOCAL

I've written programs to create spreadsheets before but never one to read
it. Research is spotty on this. Here is what I have realted to Excel:
Imports System.Data
Imports Microsoft.Office.Interop
Imports System.IO
Public Class frmTest
'Excel-related variables
'Public oXL As Microsoft.Office.Interop.Excel.Application
Public oXL As Excel.Application
Public oWB As Excel.Workbooks
Public oSheet As Excel.Worksheet
Public oRng As Excel.Range
Public indExcel As BooleanI am running VB from VS 2005.

....
'try to open the spreadsheet
Try
oXL = CreateObject("Excel.Application")
oXL.Visible = True
oWB = oXL.Workbooks.Open("c:\Trash\GL Events (3004 items).XLS")
indExcel = True
Catch myException As Exception
'If cannot connect to spreadsheet, then show error
MsgBox("Access to Spreadsheet failed.", MsgBoxStyle.Exclamation,
myException.Message)
indExcel = False
GoTo Close
End Try

Excel opens fine and the spreadsheet loads but I am getting an error
'Microsoft.Office.Interop.Excel.WorkbookClas.....' in the message box.

What have I forgotten to do?

Any help appreciated.
 
My bad. The actual error message was Unable to cast COM object of type
'Microsoft.Office.Interop.Excel.WorkbookClas...
 
As far as I can see, the error is the definition of variable oWB as a
collection of workbooks, not a workbook. if you change the lineyou will find it to work a lot better!

Regards,

Baldwin
 
Back
Top