excel interop - how to avoid late binding?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • GS

    excel interop - how to avoid late binding?

    I have installed the ms PIA for ofc XP, and followed the article

    trying to paste into a worksheet

    However I got late binding not allowed errors


    ....
    webOCWraooer,Co py // get the desired data into clapboard

    'Create a new workbook in Excel
    Dim oExcel As Object
    Dim oBook As Object
    Set oExcel = CreateObject("E xcel.Applicatio n")
    Set oBook = oExcel.Workbook s.Add ' < option strict on disallows late
    binding>


    'Paste the data
    oBook.Worksheet s(1).Range("A1" ).Select ' < option strict on disallows
    late binding>

    oBook.Worksheet s(1).Paste ' < option strict on disallows late binding>



    so I tried

    ....
    Dim oExcel As Excel.Applicati on
    Dim oBook As Excel.Workbook
    oExcel = New Excel.Applicati on
    oBook = oExcel.Workbook s.Add ' so far so good
    'Paste the data
    oBook.Worksheet s(1).Range("A1" ).Select() ' oops late binding
    error
    oBook.Worksheet s(1).Paste() ' same as above

    How do get around the last two?

    I tried
    oSheet As Excel.Worksheet s
    Dim oRange As Excel.Range

    oSheet = oBook.ActiveShe ets(1)
    but that did not help


    please Help




  • GS

    #2
    Re: excel interop - how to avoid late binding?

    Now I have cut down the errors to one:
    Dim oSheet As Excel.Worksheet
    Dim oRange As Excel.Range

    oSheet = oExcel.ActiveWo rkbook.ActiveSh eet() ' < still
    implicit conv form obj to microsoft.offic e.interop.excel .worksheet
    ' also tried oExcel.ActiveSh eet
    ' do not work: oBook.Worksheet s(1) 'oBook.Sheets

    "GS" <gsmsnews.micro soft.comGS@msne ws.Nomail.comwr ote in message
    news:Oy2WIc9rHH A.1476@TK2MSFTN GP06.phx.gbl...
    I have installed the ms PIA for ofc XP, and followed the article

    trying to paste into a worksheet
    >
    However I got late binding not allowed errors
    >
    >
    ...
    webOCWraooer,Co py // get the desired data into clapboard
    >
    'Create a new workbook in Excel
    Dim oExcel As Object
    Dim oBook As Object
    Set oExcel = CreateObject("E xcel.Applicatio n")
    Set oBook = oExcel.Workbook s.Add ' < option strict on disallows late
    binding>
    >
    >
    'Paste the data
    oBook.Worksheet s(1).Range("A1" ).Select ' < option strict on disallows
    late binding>
    >
    oBook.Worksheet s(1).Paste ' < option strict on disallows late
    binding>
    >
    >
    >
    so I tried
    >
    ...
    Dim oExcel As Excel.Applicati on
    Dim oBook As Excel.Workbook
    oExcel = New Excel.Applicati on
    oBook = oExcel.Workbook s.Add ' so far so good
    'Paste the data
    oBook.Worksheet s(1).Range("A1" ).Select() ' oops late binding
    error
    oBook.Worksheet s(1).Paste() ' same as above
    >
    How do get around the last two?
    >
    I tried
    oSheet As Excel.Worksheet s
    Dim oRange As Excel.Range
    >
    oSheet = oBook.ActiveShe ets(1)
    but that did not help
    >
    >
    please Help
    >
    >
    >
    >

    Comment

    • GS

      #3
      Re: excel interop - how to avoid late binding?

      I think I got the answer by a twisted way.
      I was stuck in vb, I went to C# got something the syntactically correct, get
      the code fragment translated back to vb using online converter and here is
      something VB will accept:

      import Microsoft.Offic e.Interop

      'Create a new workbook in Excel

      'Create a new workbook in Excel
      Dim oExcel As Microsoft.Offic e.Interop.Excel .Application
      Dim oBook As Microsoft.Offic e.Interop.Excel .Workbook
      oExcel = New Microsoft.Offic e.Interop.Excel .Application()
      oBook = New Microsoft.Offic e.Interop.Excel .Workbook()

      'Paste the data

      Dim oSheet As Microsoft.Offic e.Interop.Excel .Worksheet
      Dim oRange As Microsoft.Offic e.Interop.Excel .Range
      oSheet = DirectCast(oBoo k.ActiveSheet,
      Microsoft.Offic e.Interop.Excel .Worksheet)
      ' aha, the trick is to use directcast

      oRange = oSheet.Range("A 1")
      oSheet.Paste()


      Comment

      Working...