【问题标题】:Error 91 Using For Loop with XMLHttpRequest VBA错误 91 将 For 循环与 XMLHttpRequest VBA 一起使用
【发布时间】:2018-05-15 19:23:55
【问题描述】:

我正在尝试从网站上抓取各种 HTML 标记的内容。我正在对一组 URL 使用一组 html 标记。

Sub pArse(UserForm4_HiddenList_Text)

Dim http As New XMLHTTP60
'Dim html As New HTMLDocument
Dim hero_true_Val As Integer
Dim down_var As Integer
Dim bill_array_redim
Dim element_tag As String
Dim address_count As Integer


        hero_true_Val = Application.WorksheetFunction.CountIf(Range("B:B"), True)
        down_var = 1
        bill_array_redim = Split(UserForm4_HiddenList_Text, Chr(10))

ReDim address_array(hero_true_Val)

For Z = 2 To 6
        If Sheets("resource").Cells(Z, 2).Value <> "False" Then
            address_count = address_count + 1
            address_array(address_count) = Sheets("resource").Cells(Z, 1).Value
        End If

Next Z



Sheets("Sheet1").Select

For url_stack = 0 To UBound(bill_array_redim)
    Sheets("sheet1").Cells(down_var, 1) = bill_array_redim(url_stack)

    For what_to_check = 1 To address_count


        With http
        .Open "GET", bill_array_redim(url_stack), False
        .send
        Do While http.readyState <> 4
        DoEvents
        Loop


        End With

                Dim html As New HTMLDocument
                With html
                .body.innerHTML = http.responseText
                ' the next line is where the error occurs 
                Sheets("Sheet1").Cells(down_var, 2).Value = .querySelector(address_array(what_to_check)).innerText
                End With

                down_var = down_var + 1

    Next what_to_check
Next url_stack

UserForm4.Hide
End Sub

用户单击命令按钮,然后输入 URL 数组 (UserForm4_HiddenList_Text)。然后代码检查名为“resource”的工作表中是否存在“true”一词。这会生成一组要检查的术语。

我遇到的问题是,在 what_to_check 循环的第二次迭代中,我得到了

错误 91:未设置对象或块变量

我不确定问题出在哪里,而且似乎只有当我传入两个 html 标记而不是一个时才会发生。

编辑:感谢您回复辛迪。错误位置以粗体显示。消息是“对象或未设置块变量。”

【问题讨论】:

  • 哪一行导致了错误?请问错误信息的文本是什么?请注意,您可以使用edit 链接将其直接添加到您的问题中。
  • @CindyMeister 错误 91 将是经典的“对象变量或未设置块变量”;-)
  • 这个错误意味着你正在对一个不存在的对象进行成员调用,即它的引用是Nothing,一个空指针。你有很多链接的成员调用,这使得这个特定问题比它需要的更难调试。将address_array(what_to_check)的结果放入自己的局部变量中,然后将.querySelector(thatVariable)放入自己的局部变量中,然后将单元格值= thatOtherVariable.innerText - 然后您将确切知道哪个对象引用Nothing,您可以采取相应的行动。
  • @MathieuGuindon 非常感谢。我会研究一下。
  • 数组从 0 索引开始,最后一项是 ubound(array) -1。

标签: excel vba xmlhttprequest


【解决方案1】:

原来我收到了一个错误,我要么不完全理解,要么不太清楚。问题是响应中没有我正在寻找的标签,因为它不在网站上。通过使用:

With html
         .body.innerHTML = http.responseText
         If .body.contains(.querySelector(tag_array(tag_array_index_no))) 
Then
        Sheets(1).Range(Cells(down_var, 2).Address).Value = 
 tag_array(tag_array_index_no)
        Sheets(1).Range(Cells(down_var, 3).Address).Value = 
.querySelector(tag_array(tag_array_index_no)).innerText
        Else
        Sheets(1).Range(Cells(down_var, 2).Address).Value = 
tag_array(tag_array_index_no)
        Sheets(1).Range(Cells(down_var, 3).Address).Value = 
tag_array(tag_array_index_no) + " not set."
        End If
        End With

我可以检查响应文本中是否存在标记。

【讨论】:

    猜你喜欢
    • 2012-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-24
    • 1970-01-01
    • 2023-03-27
    相关资源
    最近更新 更多