Reputation: 43
The below macro works in IE9, but when using IE11 it stops on the Do While
statement. Also, Set HTMLDoc = ie.document
does not work for the same reason.
Note that the website will not work as it is restricted to some users only.
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
Upvotes: 1
Views: 1363
Reputation: 672
The problem with IE is that the READYSTATE never get the COMPLETED state during the do while, it happened to me many time, reading online seem to be a IE problem,
Sometime this helped me
Do While ie.Busy Or ie.readyState <> READYSTATE_COMPLETE
DoEvents
Loop
An alternative are to declare IE object with event and use the ie_documentComplete event:
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
Upvotes: 2