处理 HTMLSelectElement 时未设置对象变量或块变量

Object variable or block variable not set when processing HTMLSelectElement

我正在尝试 运行 下面的用户定义函数,但我收到以下错误:

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

我在下面附上了 VBA 代码 (source)。请检查一下,让我知道这段代码有什么问题。

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

我该如何解决这个问题?

我在逛的时候也看到了the OzGrid post you're pulling from. The problem is that the test URL, http://douglasne.mapping-online.com/DouglasCoNe/static/valuation.jsp,没有你要找的元素了!例如,它没有 <select name="StreetDir">。所以 dirSelect 在你调用 Find_Select_Option 时是 Nothing

我建议使用本地文件进行测试。例如,创建c:\users\prashant\foo.htm(或任何你想放的地方),内容如下(修改自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>

那么下面的代码应该可以工作(它对我有用):

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

getElementsByName 的参数是 "Car" 时,我得到 OK 响应。当我将该参数更改为 "Nonexistent" 时,我得到 No element found。这确认 dirSelect 在您调用 Find_Select_Option.

时代码中的 Nothing