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?