Option Strict On disallows late binding.

  • Thread starter Thread starter KC
  • Start date Start date
K

KC

I'm trying to export data to Excel, and I found an example routine at
microsoft, but I get an "Option Strict On disallows late binding." at 'oBook
= oExcel.Workbooks.Add'. Without turning Option Strict Off, how do I get rid
of this error?

The code example is below...

Dim oExcel As Object
Dim oBook As Object
Dim oSheet As Object

'Start a new workbook in Excel.
oExcel = CreateObject("Excel.Application")
oBook = oExcel.Workbooks.Add

'Add data to cells of the first worksheet in the new workbook.
oSheet = oBook.Worksheets(1)
oSheet.Range("A1").Value = "Last Name"
oSheet.Range("B1").Value = "First Name"
oSheet.Range("A1:B1").Font.Bold = True
oSheet.Range("A2").Value = "Doe"
oSheet.Range("B2").Value = "John"

'Save the Workbook and quit Excel.
oBook.SaveAs(sSampleFolder & "Book1.xls")
oSheet = Nothing
oBook = Nothing
oExcel.Quit()
oExcel = Nothing
GC.Collect()
 
No joy. Following all the instructions, with "Option Strict On", I still get
the late binding error.

Ken
 
* "KC said:
No joy. Following all the instructions, with "Option Strict On", I still get
the late binding error.

Sure you had a look at the code on the page I posted? Variables are not
simply declared as 'Object' there. Maybe explicit casts are needed
('oApp = DirectCast(Foo, Excel.Application)'). The code you posted
before doesn't need a reference to excel and will never compile with
'Option Strict On' because you are using late binding when accessing
members of variables declared as 'Object'.
 
Yea, I needed a way to declare it explicitly but didn't know how. After
playing around with it I got it to work with explicit declaration and with
'Option Strict On' (I just got back into VB and I'm trying to stay
disciplined.)

As usual, much thanks.

Ken
 
Back
Top