【问题标题】:Object variable or block variable not set when processing HTMLSelectElement处理 HTMLSelectElement 时未设置对象变量或块变量
【发布时间】:2018-01-02 14:42:01
【问题描述】:

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

对象变量或未设置块变量

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

我该如何解决这个问题?

【问题讨论】:

  • 什么是 Excel 中的HTMLSelectElement
  • 您的 selectElement 变量 Is Nothing(在即时窗口中查看)。问题出在调用此函数的代码中。具体来说,实例化 HTMLSelectElement 的代码。
  • 欢迎来到本站!查看tourhow-to-ask page 了解更多关于提出可以吸引高质量答案的问题的信息。您可以edit your question 包含更多信息。我同意@DickKusleika。您能否显示调用 Find_Select_Option 的代码?到目前为止,我看不出您发布的代码有任何明显的问题。
  • @Dick Kusleika 和 CXW,我也添加了代码。请检查并告诉我,这段代码有什么问题。提前致谢!!
  • 感谢代码更新!顺便说一句,引用您的来源(和legally required 使用您从 Stack Overflow 获得的代码)很重要。我已将 OzGrid 源添加到您的问题中,以便为您提供示例。

标签: vba excel dom nothing


【解决方案1】:

当我四处闲逛时,我还看到了the OzGrid post you're pulling from。问题是测试 URL http://douglasne.mapping-online.com/DouglasCoNe/static/valuation.jsp 不再包含您要查找的元素!例如,它没有&lt;select name="StreetDir"&gt;。所以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

【讨论】:

  • @CXW 感谢您的帮助。如果你能帮我解决我的另一个代码,我之前已经在这边发布了标题为“如何在 ASPX 站点的下拉列表中选择特定项目”,那就太好了。再次感谢!!
  • @prashant 我看了that question,但我不确定你在问什么。如果你编辑澄清,你可能会有更好的运气。顺便说一句 - 据我所知,当您回复某人时,“@”符号后面没有空格。我认为系统只会在没有空间的情况下通知该人。例如,如果您输入@,然后开始输入名称(没有空格),它会弹出一个建议的名称。但是,它并没有用空格来做到这一点(至少在我刚才的测试中)。快乐的黑客攻击!
  • 我想从该下拉列表中选择一项。我已添加该下拉列表的屏幕截图。让我知道是否足够清楚,或者您希望我放置整个源代码。谢谢!!
猜你喜欢
  • 1970-01-01
  • 2018-12-11
  • 2013-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多