Excel Worksheet assignment in VB.Net doesn't compile
- by Brian Hooper
I'm converting a VB6 application into VB.Net and having trouble with the basics. I start off with:-
Dim xl As Excel.Application
Dim xlsheet As Excel.Worksheet
Dim xlwbook As Excel.Workbook
xl = New Excel.Application
xlwbook = xl.Workbooks.Open(my_data.file_name)
xlsheet = xlwbook.Sheets(1)
but the last line doesn't compile; it reports
Option Strict On disallows implicit conversion from 'Object' to 'Microsoft.Office.Interop.Excel.Worksheet'
I can make this go away by replacing the line with
xlsheet = CType(xlwbook.Sheets(1), Excel.Worksheet)
but that does't look like the right thing to do to me. If the assignment is correct, I would have thought the object should naturally have the correct type.
So: does anyone know what the correct thing I should be doing here?