Excel Worksheet assignment in VB.Net doesn't compile

Posted by Brian Hooper on Stack Overflow See other posts from Stack Overflow or by Brian Hooper
Published on 2012-07-03T09:13:20Z Indexed on 2012/07/03 9:15 UTC
Read the original article Hit count: 193

Filed under:
|

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?

© Stack Overflow or respective owner

Related posts about vb.net

Related posts about excel