在设置对象变量IE.Document.getElement时出错

时间:2016-08-30 14:51:13

标签: html vba internet-explorer

我试图通过提供关键字“AABDG27”从网页获取一些数据,然后点击搜索按钮,最后收集搜索结果最后一项是市场数据类别= ESZ。它是一个开放的网站,所以下面的代码可以测试。下面是我的代码。它在行设置mydata = IE.Document.getElements ....

中给出错误
Sub keyword_search_result()

    Dim i As Long
    Dim IE As Object
    Dim objElement As Object
    Dim objCollection As Object
    Dim mydata As Object
    Dim myval As String

    Set IE = CreateObject("InternetExplorer.Application")
    IE.Visible = False
    IE.Navigate "http://www.platts.com/symbol-page-directories/symbol-search"

    Do
        DoEvents
    Loop Until IE.ReadyState = 3

    Do
        DoEvents
    Loop Until IE.ReadyState = 4

    IE.Document.getElementById("ctl00_ctl00_contentBody_contentMain_txtSearchSymbol").Value = "AABDG27"
    IE.Document.getElementById("ctl00_ctl00_contentBody_contentMain_btnSearch").Click

    Do
        DoEvents
    Loop Until IE.ReadyState = 4


    Set mydata = IE.Document.getElementsByClassName("divCellBottomMiddle")(0).getElementsByTagName("br")

     i = 0
     For Each objCollection In mydata
     myval = mydata(i).PreviousSibling.wholeText
     'Debug.Print i&; " "; myval
     i = i + 1
     If i = 11 Then
     'this is the case when myval string will come as "ESZ" 
     'but with lot of space
     Sheets("Sheet1").Range("A1") = Trim(Right(myval, 10))
     End If
     Next objCollection

     IE.Quit
     Set IE = Nothing


End Sub

所以应该改变什么,有没有更好的方法来利用winhttprequest / XMLHTTP或类似的东西呢?

2 个答案:

答案 0 :(得分:1)

我重新设计了代码,使其更容易理解,并且更容易找到表格中的元素。

通过将元素添加到String Array中,我改变了元素的查找方式。从这里你可以简单地选择数组中的最后一项,而不是迭代它以找到最后一项。我还将IE对象的等待部分添加到单独的Sub中以减少代码量。

这是代码,我让它在我的最终工作。

Sub keyword_search_result()
    Dim IE As Object: Set IE = CreateObject("InternetExplorer.Application")
    Dim StrArray() As String

    With IE
        .Visible = False
        .Navigate "http://www.platts.com/symbol-page-directories/symbol-search"
        'Wait for the page to load
        IELoad IE
        .Document.getElementById("ctl00_ctl00_contentBody_contentMain_txtSearchSymbol").Value = "AABDG27"
        .Document.getElementById("ctl00_ctl00_contentBody_contentMain_btnSearch").Click
        'Wait again
        IELoad IE
    End With

    'Put the string into an array, split by line breaks
    StrArray = Split(IE.Document.getElementsByClassName("divCellBottomMiddle")(0).InnerText, vbCrLf)

    'Retrieve the last element in the array, place in Excel Range A1
    Sheets("Sheet1").Range("A1") = StrArray(UBound(StrArray))

    'Clean Up
    IE.Quit
    Set IE = Nothing
End Sub

Public Sub IELoad(Browser As Object)

    While Browser.ReadyState <> 4 Or Browser.Busy
        Application.Wait (Now() + TimeValue("00:00:01"))
    Wend

End Sub

答案 1 :(得分:1)

另一个想法是检查HTML-Element-Collection是否为Nothing以及它是否包含某些元素。否则会发生错误,因为当使用&#39; search&#39;找不到任何内容时,访问不在集合中的元素。

Dim cellBottomMiddle ' HTML-Element-Collection 
Set cellBottomMiddle = IE.Document.getElementsByClassName("divCellBottomMiddle")

If cellBottomMiddle Is Nothing Then GoTo finalize
If cellBottomMiddle.Length <= 0 Then GoTo finalize

Set mydata = cellBottomMiddle(0).getElementsByTagName("br")

' Some code here ...

finalize:
    IE.Quit
    Set IE = Nothing