2017-09-10 62 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

相關問題