2017-09-10 61 views
0

下面的宏在IE9中工作,但是当使用IE11时它停止在Do While语句上。此外,Set HTMLDoc = ie.document也不会出于同样的原因。READYSTATE永远不会获得完成状态

请注意,该网站将无法正常工作,因为它仅限于某些用户。

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

与IE浏览器的问题是,readyState的在做,而期间从来没有完成的状态,它发生在我许多时间,阅读网上似乎是一个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 
+0

不错的一个!我如何使它与我想要访问不同域中的元素的多个选项卡一起工作 - 只需向IF块添加另一个案例? – PatricK

+0

谢谢你的回复。该宏没有运行,因为声明了'Dim WithEvents ie InternetExplorer'。即使我已经在引用中勾选了Microsoft Internet控件,但它仍然以红色显示。感谢您的时间 – Dawood

+0

@达伍德,您是否引用了Microsoft Internet Controls? – exSnake

相关问题