title: Getting the ReadyState of an XmlHttpRequest from Javascript in VBA
body: <p>I have an Excel file that we keep for things we find around my job.</p>
  <p>We have to log into a website in order to check certain things. With VBA, I've gotten an IE window to open, I get it to log in to the website and fill out a form (and submit it!).</p>
  <p>However, my problem arises because the form is submitted using Javascript and AJAX. So, I can therefore not get the response data that comes out (which is what I started this whole endeavor for).</p>
  <p><strong>My question is thus:</strong> is there <em>any</em> way I can loop in VBA until the Javascript XML object gets the data, or am I totally insane for trying?</p>
  <p>Edit: due to a request, I am putting the code I have currently. I cannot post both the items that say [removed] due to our privacy agreement. I know for a fact that once the response data is loaded it is appended to a div that has an ID of &quot;resultsTable&quot; However, my attempts to loop until that DOM element was found did not work.</p>
  <p><strong>EDIT #2</strong> I finally got something working. What was going wrong in my original code was I didn't know how the ohtml.getElementById() function worked and what it returned if it found nothing. The following waits until the data set appears. It then does certain actions with the data (which I can't post, unfortunately). Thanks everyone!</p>
  <h1>Code</h1>
  <pre><code>Private Sub IE_Autiomation()
      Dim i As Long, r As Range
      Dim IE As InternetExplorer, ohtml As HTMLDocument
      Dim ouser As HTMLInputElement, opass As HTMLInputElement, otext As HTMLTextAreaElement, otable As HTMLTable
      
      ' Create InternetExplorer Object
      
      Set r = Application.Union(ActiveSheet.ListObjects(1).Range, ActiveSheet.ListObjects(2).Range, ActiveSheet.ListObjects(3).Range, ActiveSheet.ListObjects(4).Range)
      Debug.Print r.Address
  
      Set r = Application.Intersect(r, ActiveSheet.Range(&quot;B:B,E:E,H:H&quot;)) 'Just Trust Me, Ok?
      
      Set IE = New InternetExplorer
  
   
      ' Send the form data To URL As POST binary request
      IE.Navigate &quot;[Removed]&quot;
      
      
      Do While IE.Busy
          Application.Wait DateAdd(&quot;s&quot;, 1, Now)
      Loop
      
      Set ohtml = IE.document
      Set ouser = ohtml.getElementById(&quot;USER&quot;)
      Set opass = ohtml.getElementById(&quot;PASSWORD&quot;)
      ouser.Value = &quot;[removed]&quot;
      
      
      pw = InputBox(&quot;Password?&quot;)
      opass.Value = pw
      IE.Visible = True
      ouser.form.submit
      
      Do While IE.Busy
          Application.Wait DateAdd(&quot;s&quot;, 1, Now)
      Loop
      
      Set otext = ohtml.getElementsByName(&quot;vin&quot;).Item(0)
      For Each e In r
          If e.Offset(0, -1).Value = &quot;&quot; And Not e.Value = &quot;&quot; Then
              otext.Value = otext.Value &amp; e.Value &amp; &quot;, &quot;
          End If
      Next
          Debug.Print otext.Value
          otext.Value = Left(otext.Value, Len(otext.Value) - 2)
          Debug.Print otext.Value
      
      Debug.Print &quot;So far, wafles.&quot;
      
      For Each j In ohtml.getElementsByTagName(&quot;input&quot;)
          If j.Value = &quot;Submit&quot; Then
              Set ouser = j
              Exit For
          End If
      Next
      ouser.Click 
      'Here is where I need to wait for the response to load into the HTML
      
      ' Wait while IE loading...
      
      ' Clean up
  
      Set otable = ohtml.getElementById(&quot;resultsTable&quot;)
      
      Do While otable Is Nothing
          Application.Wait DateAdd(&quot;s&quot;, 1, Now)
          Set otable = ohtml.getElementById(&quot;resultsTable&quot;)
      Loop
      
      'IE.Quit
      Set IE = Nothing
      Set objElement = Nothing
      Set ohtml = Nothing
      Set objCollection = Nothing
   
      Application.StatusBar = &quot;&quot;
  End Sub
  </code></pre>