运行时错误'91'(未设置对象变量或With块变量) [英] Run-time error '91' (Object variable or With block variable not set)

查看:806
本文介绍了运行时错误'91'(未设置对象变量或With块变量)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对VBA还是比较陌生,我试图将一个msgbox组合在一起,以便从Web抓取中给我一个特定的数字,但是我一直遇到运行时错误"91",而我根本无法弄清楚如何解决这个问题.我已经搜索了无数个stackoverflow问题,youtube视频和通用的google搜索,但是我自己并没有成功找到错误.

I am relatively new to VBA and am trying to put together a msgbox that will give me a specific number from a web scrape, however I keep running into a run-time error '91' and I simply cannot figure out how to fix this. I have searched countless stackoverflow questions, youtube videos and generic google searches, however have not been successful in finding out the error on my own.

这是代码:

Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate ("http://brokercheck.finra.org")
Do
DoEvents
Loop Until IE.ReadyState = 4

'Enter values from the corresponding sheet
'Set some generic typing for ease
Set doc = IE.document

    doc.GetElementbyID("GenericSearch_IndividualSearchText").Value = Worksheets("Master").Range("D203")
    doc.GetElementbyID("GenericSearch_EmploymingFirmSearchText").Value = Worksheets("Master").Range("C203")

    Set elements = doc.getElementsByTagName("button")
    For Each element In elements
    If element.getAttribute("type") = "submit" Then
        element.Click
        Exit For
    End If
    Next element

Do
DoEvents
Loop Until IE.ReadyState = 4

'find CRD#
    Set crd = doc.getElementsByClassName("summarydisplaycrd")(0).innerText 'here is where the run time error occurs
    MsgBox crd

和HTML,我正在尝试从以下信息中获取信息:

and the HTML I am trying to get the information from:

<div class="searchresulttext">
    <div class="bcrow">
        <div class=""> <span class="summarydisplayname">[redacted]</span> <span class="summarydisplaycrd text-nowrap">(CRD# 5944070)</span></div>

推荐答案

我正在审查此代码和finra.org网站,并具有以下发现,当解决这些问题时,应该可以解决该问题.

I'm reviewing this code and the finra.org site, and have the following observations, which when addressed, should resolve the problem.

  1. 根据从检查"按钮返回的实际HTML,您提供的HTML示例完全不正确.

  1. The HTML example you provided is simply incorrect, based on the actual HTML that is returned from the "Check" button.

返回的实际HTML看起来像这样,并且类名是"displayname",而不是"summarydisplaycrd":

The actual HTML returned looks like this, and the classname is "displayname", not "summarydisplaycrd":

<div class="SearchResultItemColor bcrow">
    <div class="searchresulttext">
        <div class="bcsearchresultfirstcol">
                <span class="displayname">[redacted]</span> <span class="displaycrd">(CRD# 123456789)</span>

  1. 找到 first 提交"按钮后,您的代码退出For each element循环. 可能不是检查"按钮(尽管我可以通过任何一种方式获得结果,但是您可能希望在代码中添加更多逻辑以确保检查"按钮已提交.
  1. Your code exits the For each element loop upon finding the first "submit" button. This may not be the "Check" button (although I can get results either way, you may want to add more logic in the code to ensure the "Check " button is submit.

更新

在进一步检查中,尽管我可以复制91型错误,但我仍然不知道为什么您的类名无论如何都显得与我的不同(也许是IE11的东西,不知道...),但我能够通过强制较长的延迟来解决该问题,因为在这种情况下DoEvents循环根本不够用(有时在这种情况下,当外部函数动态提供数据时,浏览器为ReadyState = 4和.Busy = True,因此循环不执行任何操作)

On further review, while I can replicate the Type 91 error, I still don't know why your class name appears different than mine (maybe an IE11 thing, dunno...) in any case, I'm able to resolve that by forcing a longer delay, as in this case the DoEvents loop is simply not adequate (sometimes this is the case when data is served dynamically from external functions, the browser is ReadyState=4 and .Busy=True, so the loop doesn't do anything)

我使用WinAPI Sleep功能,并在按下"Click"按钮后强制延迟1秒,在ReadyState = 4 .Busy=True的条件下循环播放.

I use the WinAPI Sleep function and force a 1 second delay after the "Click" button pressed, looping on condition of ReadyState = 4 and .Busy=True.

注意,您将需要根据classname参数在HTML上的显示方式进行修改.

NOTE you will need to modify the classname parameter depending on how it is appearing on your HTML.

Option Explicit
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Sub finra()
Dim IE As Object
Dim doc As Object, element As Object, elements As Object, crd

Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate ("http://brokercheck.finra.org")

Call WaitIE(IE, 1000)

'Enter values from the corresponding sheet
'Set some generic typing for ease
Set doc = IE.document

    doc.GetElementbyID("GenericSearch_IndividualSearchText").Value = "steve"
    doc.GetElementbyID("GenericSearch_EmploymingFirmSearchText").Value = "ed"

    Set elements = doc.getElementsByTagName("button")
    For Each element In elements
    If element.getAttribute("type") = "submit" Then
        If element.innerText = "Check " Then
            element.Click
            Exit For
        End If
    End If
    Next element


Call WaitIE(IE, 1000)

Dim itms As Object
'Set itms = doc.getElementsByClassName("displaycrd")
    crd = doc.getElementsByClassName("displaycrd")(0).innerText 'here is where the run time error occurs
    MsgBox crd
End Sub


Sub WaitIE(IE As Object, Optional time As Long = 250)
Dim i As Long
Do
    Sleep time
    Debug.Print CStr(i) & vbTab & "Ready: " & CStr(IE.ReadyState = 4) & _
                vbCrLf & vbTab & "Busy: " & CStr(IE.Busy)
    i = i + 1
Loop Until IE.ReadyState = 4 And Not IE.Busy
End Sub

这篇关于运行时错误'91'(未设置对象变量或With块变量)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆