title: VBA Internet Explorer wait for web page to load
body: <p>I know questions like this have been asked before, but mine is a bit different and has been fairly troubling.  What I'm dealing with is a web page with a form with a few events that load more of the page when certain items in input boxes are filled out.  When these events fire the page loads again, but remains at the same URL with the same nameprop.  I've been using the following types of methods both seperately and strung together to handle waiting for the page to load, but sometimes the VBA still manages to continue executing and set the HTMLDocument variable to a page without the appropriate information on it causing the macro to debug.  Here are the kinds of things I've been trying so far:</p>
  
  <pre><code>While IE.Busy
      DoEvents
  Wend
  
  Do Until IE.statusText = "Done"
      DoEvents
  Loop
  
  Do Until IE.readyState = 4
      DoEvents
  Loop
  </code></pre>
  
  <p>I've even attempted to place these events into a loop like the following, but it didn't quite work because the lastModified property only returns a value down to the second and the macro spins through the fields fast enough that it is returning a new page in the same second:</p>
  
  <pre><code>Do Until IE.statusText = "Done" And IE.Busy = False And IE.ReadyState = 4 _
  And IE.document.lastModified &gt; 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
  </code></pre>
  
  <p>Even that fails to wait long enough to set the HTMLDocument object leading to a debug.  I've contemplated setting the next input element and checking that for nothing to further the code, but even that wouldn't be successful 100% of the time because generally the Input elements exist in the HTML but are hidden until the appropriate event is fired, which wouldn't be a problem but they don't load their possible selections until after the event is fired.  It might be an odd page.</p>
  
  <p>Anyway... not sure what else to add.  If there is something else that might be helpful to see just ask.  I guess what I'm looking for is a way to get VBA to wait until IE knows another page isn't on it's way.  It seems to load a few times before it is completely done.</p>
  
  <p>So... Anyone have any ideas?</p>
  
  <p>EDIT:  Found a few new things to try.  Still, no dice.  It was suggested that I add these attempts.  Here is the code, for some reason the VBE and excel instance become non-responsive when using this approach after firing an event that should populate the options on the select element... thinking about trying xml... here is the code:</p>
  
  <pre><code>intCounter = 0
  Do until intCounter &gt; 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
  </code></pre>
  
  <p>Based on what I can see happening on the web page, I know that it is somewhere in this loop that the VBE and Excel become non-responsive.  </p>
  
  <p>Hope that helps... I know it didn't help me... Drats.</p>
  
  <p>EDIT:  Just thought I'd add this.  When it comes to automating a web page, for the most part, I no longer use IE.  I've found it's much better, and sidesteps this issue of async stuff entirely, to simply perform the posts and gets yourself.  May not be the best solution depending on what you're trying to do, but it works pretty reliably if you look at the traffic closely and parameterize things well.</p>