Search code examples
excelvbadom

Object variable or block variable not set when processing HTMLSelectElement


I'm trying to run the below user defined function, but I receive the following error:

object variable or with block variable not set

Private Function Find_Select_Option(selectElement As HTMLSelectElement, optionText As String) As Integer

    Dim i As Integer

    Find_Select_Option = -1
    i = 0
    While i < selectElement.Options.length And Find_Select_Option = -1 ' ### error occurs on this line
        DoEvents
        If LCase(Trim(selectElement.Item(i).Text)) = LCase(Trim(optionText)) Then Find_Select_Option = i
        i = i + 1
    Wend

End Function

I have attached the VBA code below (source). Please go through it and let me know, what's wrong in this code.

Public Sub IE1()

    Dim URL As String
    Dim IE As InternetExplorer
    Dim HTMLdoc As HTMLDocument

    URL = "http://douglasne.mapping-online.com/DouglasCoNe/static/valuation.jsp"

    Set IE = New InternetExplorer
    With IE
        .Visible = True
        .navigate URL
        While .Busy Or .readyState <> READYSTATE_COMPLETE
            DoEvents
        Wend
        Set HTMLdoc = .document
    End With

    '<select name="StreetDir">
    Dim optionIndex As Integer
    Dim dirSelect As HTMLSelectElement
    Set dirSelect = HTMLdoc.getElementsByName("StreetDir")(0)
    'dirSelect.selectedIndex = 2            'set option index directly
    optionIndex = Find_Select_Option(dirSelect, "E")
    If optionIndex >= 0 Then
        dirSelect.selectedIndex = optionIndex
    End If

    '<select name="StreetSfx">
    Dim suffixSelect As HTMLSelectElement
    Set suffixSelect = HTMLdoc.getElementsByName("StreetSfx")(0)
    optionIndex = Find_Select_Option(suffixSelect, "PLAZA")
    If optionIndex >= 0 Then
        suffixSelect.selectedIndex = optionIndex
    End If

End Sub

How can I fix this?


Solution

  • When I was poking around I also saw the OzGrid post you're pulling from. The problem is that the test URL, http://douglasne.mapping-online.com/DouglasCoNe/static/valuation.jsp, no longer has the elements you are looking for! For example, it does not have <select name="StreetDir">. So dirSelect is Nothing at the time you call Find_Select_Option.

    I recommend testing with a local file. For example, create c:\users\prashant\foo.htm (or wherever you want to put it) with the following contents (modified from w3schools):

    <!DOCTYPE html>
    <html>
    <body>
    
    <select name="Car">
      <option value="volvo">Volvo</option>
      <option value="saab">Saab</option>
      <option value="opel">Opel</option>
      <option value="audi">Audi</option>
    </select>
    
    </body>
    </html>
    

    Then the following code should work (it does for me):

    Public Sub IE1()
    
        Dim URL As String
        Dim IE As SHDocVw.InternetExplorer
        Dim HTMLdoc As MSHTML.HTMLDocument
    
        URL = "c:\users\prashant\foo.htm"    ' *** Read from a local file
    
        Set IE = New InternetExplorer
        With IE
            .Visible = True
            .navigate URL
            While .Busy Or .ReadyState <> READYSTATE_COMPLETE
                DoEvents
            Wend
            Set HTMLdoc = .document
        End With
    
        '<select name="Car">
        Dim optionIndex As Integer
        Dim dirSelect As HTMLSelectElement
        Dim message As String
    
        Set dirSelect = Nothing   ' *** Set up for the error checking below
        On Error Resume Next
    
        'Set dirSelect = HTMLdoc.getElementsByTagName("select").Item(0) ' This is OK, too
        Set dirSelect = HTMLdoc.getElementsByName("Car").Item(0)  ' *** It exists!
    
        ' *** Here's some error-checking code you can use
        If Err.Number <> 0 Then         ' Report errors
            message = "Error " & CStr(Err.Number) & vbCrLf & Err.Description
        ElseIf dirSelect Is Nothing Then
            message = "No element found"
        Else
            message = "OK" & vbCrLf & dirSelect.textContent
        End If
        On Error GoTo 0   ' *** Back to normal
    
        MsgBox message
    End Sub
    

    When the parameter to getElementsByName is "Car", I get an OK response. When I change that parameter to "Nonexistent", I get No element found. This confirms that dirSelect is Nothing in your code at the point you call Find_Select_Option.