【问题标题】:READYSTATE never get the COMPLETED stateREADYSTATE 永远不会获得 COMPLETED 状态
【发布时间】:2017-09-11 21:11:15
【问题描述】:

以下宏在 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

【问题讨论】:

    标签: vba internet-explorer


    【解决方案1】:

    IE的问题是在do while中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
    

    【讨论】:

    • 不错的一个!我如何使它适用于我想要访问来自不同域的元素的多个选项卡 - 只需将另一个案例添加到 IF 块?
    • 感谢您的回复。由于声明了Dim WithEvents ie As InternetExplorer,宏未运行。当我想运行宏时,它显示为红色,即使我已经在引用中勾选了 Microsoft Internet 控件。感谢您的时间
    • @Dawood,你引用了 Microsoft Internet Controls 吗?
    • @PatricK 每次 ie 对象完全加载 dom 时都会触发此事件,我想到了 atm 2 解决方案,一个是像你说的那样添加另一个 if 块,但你必须更改相同的 ie实例到第二个 url,或创建另一个 InternetExplorer 实例以不同的名称(可能是 ie2)并创建第二个名为 ex 的子:ie2_DocumentComplete(ByVal pDisp as Object, URL as Variant)
    • 是的,我做到了。而且,当我将WithEventsie 放在没有空格的位置时,它向前移动但停在Set ie = New InternetExplorer 并说变量未定义
    猜你喜欢
    • 2019-02-05
    • 1970-01-01
    • 2021-03-08
    • 1970-01-01
    • 2021-10-27
    • 1970-01-01
    • 1970-01-01
    • 2021-11-09
    • 1970-01-01
    相关资源
    最近更新 更多