title: Wait Until Website Internal Search Has Completed Using VBA
body: <p>I am really struggling to get my VBA code to wait until a website I am accessing, finishes an internal search it is performing.</p>
  
  <p>So far I have tried: </p>
  
  <pre><code>Do While ie.Busy: DoEvents: Loop     
  
  Set doc = ie.document     
  
  Do While doc.readyState &lt;&gt; "complete": DoEvents: Loop
  </code></pre>
  
  <p>but it does not work. I could post the html 'refresh' function if that would help?</p>
  
  <p>Any ideas would be greatly appreciated.</p>
  
  <p><strong>EDIT</strong>
  It is using the following function which I have gathered from the source code: </p>
  
  <pre><code>var limit="5"; 
  function beginrefresh()
  { 
  if (!document.images) return;
  if (limit==1) 
      document.getElementsByTagName("Form")[0].submit(); 
  else
     { 
        limit-=1; 
        window.status="Page will refresh in " + limit + " seconds."; 
        setTimeout("beginrefresh()",1000);
     } 
  } 
  
  window.onload = function () { beginrefresh(); }
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>I have cracked it and it was really simple in the end. I used the following to search for a known id on the page that was being loaded. So when it found this id it knew that the code could continue. A very useful technique IF you know an the name of an id on the page loading.</p>
  
  <pre><code>Do
  
  
  Do While ie.Busy: DoEvents: Loop
  
      Set doc = ie.document
  
  Do While doc.readyState &lt;&gt; "complete": DoEvents: Loop
  
  
  Set testobject = ie.document.getElementById("changeFilterCriteria")
  
     If Not testobject Is Nothing Then Exit Do
  </code></pre>
  
  <p>Loop</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Or maybe not - it does work for about 5 or 6 times in a row but then crashes on</p>
  
  <pre><code>Set testobject = ie.document.getElementById("changeFilterCriteria")
  </code></pre>
  
  <p>IE comes up with 'internet explorer cannot display the webpage',</p>
  
  <p>If I then click back and then refresh the page is still performing its internal search.</p>
  
  <p>Any help would be gratefully received! </p>
  
  <p>Jim</p>