I know that such questions have been asked before, but mine are a bit different and were quite disturbing. I am dealing with a web page with a form with several events that load most of the page when some elements in the input field are filled. When these events reload the page again, but remain with the same url with the same prop name. I use the following types of methods, both separately and in combination with each other, to handle waiting for the page to load, but sometimes VBA still manages to continue execution and set the HTMLDocument variable to the page without the corresponding information about it, resulting in a macro for debugging. Here is what I am doing so far:
While IE.Busy DoEvents Wend Do Until IE.statusText = "Done" DoEvents Loop Do Until IE.readyState = 4 DoEvents Loop
I even tried putting these events in a loop, as shown below, but it didnβt work because the lastModified property only returns the value to the second, and the macro rotates through the fields quickly enough, returning a new page in one second:
Do Until IE.statusText = "Done" And IE.Busy = False And IE.ReadyState = 4 _ And IE.document.lastModified > LastModified ----or---- IE.document.nameprop = _ "some known and expected name prop here" While IE.Busy DoEvents Wend Do Until IE.statusText = "Done" DoEvents Loop Do Until IE.readyState = 4 DoEvents Loop Loop
Even this does not allow you to wait long enough to set the HTMLDocument object, leading to debugging. I assumed to install the next input element and verify that nothing contributes to further coding, but even this will not be successful for 100% of the time, because usually the input elements exist in HTML, but are hidden until the corresponding event is fired, which will not be a problem, but they do not load their possible choices until the event is fired. It could be a weird page.
In any case ... not sure what else to add. If there is anything else that might be helpful, just ask. I guess what I'm looking for is a way to make VBA wait until IE finds out that the other page is not on it. It seems to load several times before it is fully executed.
So ... Does anyone have any ideas?
EDIT: Found some new things to try. However, there are no dice. It has been suggested that I am adding these attempts. Here is the code, for some reason, the VBE and excel instance becomes immune when using this approach after triggering an event that should populate the parameters in the select element ... think of an xml attempt ... here is the code:
intCounter = 0 Do until intCounter > 2 Do Until IE.Busy = False: DoEvents: Loop Do Until IE.ReadyState = 4: DoEvents: Loop Set HTMLDoc = IE.Document Do Until HTMLDoc.ReadyState = "complete" Set HTMLSelect = HTMLDoc.getElementById("ctl00$ctl00$MainContent$ChildMainContent$ddlEmployeeBranchCodes") intCounter = 0 For each Opt in HTMLSelect intCounter = intCounter + 1 Next Opt Loop
Based on what I see on the web page, I know that it is in this cycle that VBE and Excel become immune.
Hope this helps ... I know it didn't help me ... Drats.
EDIT: Just thought I'd add this. When it comes to webpage automation, for the most part, I no longer use IE. I found this much better and completely circumvented this issue of asynchronous stuff by simply doing the recordings and getting it myself. It may not be the best solution, depending on what you are trying to do, but it works quite reliably if you carefully look at the traffic and evaluate the parameters well.