READYSTATE永远不会获得COMPLETED状态

时间:2017-09-10 20:57:22

标签: vba internet-explorer

以下宏在IE9中有效,但在使用IE11时,它会在Do While语句中停止。此外,Set HTMLDoc = ie.document因同样的原因无效。

请注意,该网站无法使用,因为它仅限于某些用户。

Option Explicit

Sub GetHTMLDocument()

Dim ie As New SHDocVw.InternetExplorer
Dim HTMLDoc As MSHTML.HTMLDocument
Dim HTMLInput As MSHTML.IHTMLElement
Dim htmlbuttons As MSHTML.IHTMLElementCollection
Dim htmlbutton As MSHTML.IHTMLElement


ie.Visible = True
ie.navigate "siiprodsrs01.db.sma"

Do While ie.readyState <> READYSTATE_COMPLETE

Loop


Set HTMLDoc = ie.document

Set HTMLInput = HTMLDoc.getElementById("what")
HTMLInput.Value = "12345"

Set htmlbuttons = HTMLDoc.getElementsByTagName("button")

For Each htmlbutton In htmlbuttons
Debug.Print htmlbutton.className, htmlbutton.tagName, htmlbutton.ID, htmlbutton.innerText

Next htmlbutton

htmlbuttons(0).Click

End Sub

1 个答案:

答案 0 :(得分:1)

IE的问题是READYSTATE在执行期间永远不会得到COMPLETED状态,它发生在我很多时候,在线阅读似乎是一个IE问题,

有时这对我有帮助

Do While ie.Busy Or ie.readyState <> READYSTATE_COMPLETE
    DoEvents
Loop

另一种方法是使用事件声明IE对象并使用ie_documentComplete事件:

Option Explicit
'Requires Microsoft Internet Controls Reference Library
Dim WithEvents ie As InternetExplorer
Sub start_here()
    Set ie = New InternetExplorer
    ie.Visible = True
    'First URL to go, next actions will be executed in
    'Webbrowser event sub procedure - DocumentComplete
    ie.Navigate "siiprodsrs01.db.sma"
End Sub

Private Sub ie_DocumentComplete(ByVal pDisp As Object, URL As Variant)
    'pDisp is returned explorer object in this event
    'pDisp.Document is HTMLDocument control that you can use
    Set HTMLDoc = pDisp.Document
    'Since there is no do-loop, we have to know where we are by using some reference
    If InStr(1, URL, "siiprodsrs01.db.sma") > 0 Then
        Set HTMLInput = pdDisp.document.getElementById("what")
        HTMLInput.Value = "12345"

        Set htmlbuttons = HTMLDoc.getElementsByTagName("button")

        For Each htmlbutton In htmlbuttons
            Debug.Print htmlbutton.className, htmlbutton.tagName, htmlbutton.ID, 
            htmlbutton.innerText
        Next htmlbutton

        htmlbuttons(0).Click

    End If
End Sub