VBA hanging on ie.busy and readystate check
In Parse_Team_RawSalaries
, instead of using the InternetExplorer.Application
object, how about using MSXML2.XMLHTTP60
?
So, instead of this:
Set IE = CreateObject("InternetExplorer.Application")IE.Visible = FalseIE.navigate "http://www.rotoworld.com/teams/contracts/nfl/" & TeamWhile IE.Busy Or IE.ReadyState <> READYSTATE_COMPLETE: DoEvents: WendSet HTMLdoc = IE.Document
Maybe try using this (add a reference to "Microsoft XML 6.0" in VBA Editor first):
Dim IE As MSXML2.XMLHTTP60Set IE = New MSXML2.XMLHTTP60IE.Open "GET", "http://www.rotoworld.com/teams/contracts/nfl/" & Team, FalseIE.sendWhile IE.ReadyState <> 4 DoEventsWendDim HTMLDoc As MSHTML.HTMLDocumentDim HTMLBody As MSHTML.htmlBodySet HTMLDoc = New MSHTML.HTMLDocumentSet HTMLBody = HTMLDoc.bodyHTMLBody.innerHTML = IE.responseText
I've generally found that MSXML2.XMLHTTP60
(and WinHttp.WinHttpRequest
, for that matter) generally perform better (faster and more reliable) than InternetExplorer.Application
.
I've found this post very helpful when I encountered similiar problem. Here is my solution:
I used
Dim browser As SHDocVw.InternetExplorerSet browser = New SHDocVw.InternetExplorer
and
cTime = Now + TimeValue("00:01:00")Do Until (browser.readyState = 4 And Not browser.Busy) If Now < cTime Then DoEvents Else browser.Quit Set browser = Nothing MsgBox "Error" Exit Sub End IfLoop
Sometimes page is loaded but code stops on DoEvents and goes on and on and on. Using this code it goes on only for 1 minute and if browser is not ready it quits the browser and exits sub.
I know this is a old post but. I have had the same problem with my code for downloading web site pictures using Excel VBA automation. Some sites wont let you download a image file using a link without first opening the link in a browser. However my code was getting hung up sometimes with when the objBrowser.visible was set to false with the folowing code
Do Until (objBrowser.busy = False And objBrowser.readyState = 4) Application.Wait (Now + TimeValue("0:00:01")) DoEvents 'browser.readyState = 4Loop
the simple fix was to make the objBrowser.visible I fixed it with
Dim Passes As Integer: Passes = 0 Do Until (objBrowser.busy = False And objBrowser.readyState = 4) Passes = Passes + 1 'count loops Application.Wait (Now + TimeValue("0:00:01")) DoEvents If Passes > 5 Then 'set size browser cannot set it smaller than 400 objBrowser.Width = 400 'set size objBrowser.Height = 400 Label8.Caption = Passes 'display loop count ' position browser "you cannot move it off the screen" ready state wont change objBrowser.Left = UserForm2.Left + UserForm2.Width objBrowser.Top = UserForm2.Top + UserForm2.Height objBrowser.Visible = True DoEvents objBrowser.Visible = False End If Loop
objBrowser only flashes for less than a second but it gets the job done!