Results 1 to 7 of 7

Thread: [RESOLVED] [2005] Late Binding

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    394

    Resolved [RESOLVED] [2005] Late Binding

    I'm opening an Excel file and using the header row to fill a list box.

    The code below works until I turn on the project setting to disallow late binding. Then line in bold is producing a problem. Is there an easier way to do what I want without having to turn off the late binding warning/error?

    Code:
            Dim strList As String = ""
            Dim i As Integer = 1
            Dim oSht As New Excel.Worksheet
            Dim oWB As Excel.Workbook
    
            Try
                'Create an Excel instance.
                oApp = New Excel.Application
                oWB = oApp.Workbooks.Open("c:\temp\test.xls")
                oSht = CType(oWB.ActiveSheet, Excel.Worksheet)
    
                Do
                    strList = CType(oSht.Cells(1, i).value, String)
                    If strList = "" Then
                        Exit Do
                    End If
                    lstAllVars.Items.Add(strList)
                    i = i + 1
                Loop While strList <> ""
    
                'close and clean up resources
                oApp.Quit()
                oApp = Nothing
            Catch ex As Exception
                MessageBox.Show(ex.Message.ToString)
            End Try

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] Late Binding

    What exactly is being late-bound? It's not the strList variable because you're casting the value you're assigning to it. I can only assume that the result of oSht.Cells(1, i) is type Object and therefore doesn't have a Value property. You'd have to cast that reference to the appropriate type before trying to access its Value property.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    394

    Re: [2005] Late Binding

    the result of oSht.Cells(1, i) is type Object and therefore doesn't have a Value property.
    I'm not in front of my code right now but I think that you're right about the oSHt.Cells reference. I believe that if I turn the late binding errror flag (don't know what it is really called) that the .value method was no longer available.

    So you're saying that I should try?
    Code:
    CType(oSht.Cells(1, i),string).value
    I'll give it a shot when I get to work tomorrow.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] Late Binding

    It's called Option Strict.

    That's not what I'm saying. I'm saying that you need to cast the result of oSht.Cells(1, i) as the actual type it is first, then access the Value property of that type, then cast that as type String. It might look something like:
    vb Code:
    1. strList = CStr(CType(oSht.Cell(1, i), Excel.Cell).Value))
    Having said that, I'm surprised that that Cells property doesn't return a typed reference. I've not really done much Office automation so I'm really not sure of all the specifics.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    394

    Re: [2005] Late Binding

    strList = CStr(CType(oSht.Cell(1, i), Excel.Cell).Value))
    I remoted into my work PC and tried your suggestion. Excel.Cell is an undefined type. I'm missing something rudimentary.

    This will teach me to get half way through a project and decide to change compile options.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    394

    Re: [2005] Late Binding

    Code:
    strList = CStr(CType(oSht.Cells(1, i), Excel.Range).Value)
    This got it. thanks

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] Late Binding

    I just made up "Excel.Cell" because it sounded like about the right thing. Notice that I said:
    It might look something like
    Look at the Excel Object Model documentation and see what the appropriate type is.

    Edit: Too slow
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width