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?
It is not possible without a conversion of type (If Option Strict Is On) because .NET cannot understand a VBA Collection.
I believe this bug report is related to the issue:
If you have a look at the Excel Example on this page (Converting Code from VBA to Visual Basic .NET), then it shows this code:
There have been several post about this issue like here and here but there doesn’t seem to be a better way round it.