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

问题描述 投票:2回答:1

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

对象变量或未设置块变量

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

我怎样才能解决这个问题?

vba excel-vba dom nothing excel
1个回答
0
投票

当我在四处寻找时,我也看到了the OzGrid post you're pulling from。问题是测试网址http://douglasne.mapping-online.com/DouglasCoNe/static/valuation.jsp不再具有您正在寻找的元素!例如,它没有<select name="StreetDir">。所以当你打电话给dirSelect时,NothingFind_Select_Option

我建议使用本地文件进行测试。例如,使用以下内容创建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在您的代码中是Nothing,在您调用Find_Select_Option时。

© www.soinside.com 2019 - 2024. All rights reserved.