title: (VBA) How to refresh HTMLDocument object when navigating new html page on same browser?
body: <p>I am tasked with creating a macro that logs into a website and navigates to a specific page where a html table can be copied into excel. My problem is that when I try to pull the html table, I get the html table of the previous webpage. My thinking is I need to update my HTMLDocument some how. Is this possible? I unfortunately can't give the specific URL as it's confidential.</p>
  
  <p>My code can be found below.</p>
  
  <pre><code>Sub website_login()
  
  'variables
  Dim HTMLDoc As HTMLDocument
  Dim Browser As InternetExplorer
  Dim HTML_Element As IHTMLElement
  Dim URL As String
  Dim l As Object
  
  On Error GoTo Err_Clear
  URL = "MY URL"
  
  Set Browser = New InternetExplorer
      Browser.Silent = True
      Browser.navigate URL
      Browser.Visible = True
  
  With Browser
  Do While .Busy Or .readyState &lt;&gt; 4
      DoEvents
  Loop
  End With
  
  Set HTMLDoc = Browser.document
  
  'fill in login credentials
  HTMLDoc.getElementById("username").Value = "USERNAME" 
  HTMLDoc.getElementById("password").Value = "PASSWORD"
  
  'loop through collection of INPUT tags and Login In
  For Each HTML_Element In HTMLDoc.getElementsByTagName("input")
      If HTML_Element.Value = "Login" Then
          HTML_Element.Click
          Exit For
      End If
  Next
  
  'will be brought back to login if delays aren't added
  Application.Wait (Now + TimeValue("0:00:05")) ' delay 5 seconds
  Application.Wait (Now + TimeValue("0:00:05")) ' delay 5 seconds
  
  Browser.navigate ("NEW URL")'navigate to new webpage on same website
  
  
  'BELOW CODE COPIES TABLE INTO EXCEL
  'http://www.ozgrid.com/forum/showthread.php?t=184695
  Dim eleColtr As MSHTML.IHTMLElementCollection 'Element collection for tr tags
  Dim eleColtd As MSHTML.IHTMLElementCollection 'Element collection for td tags
  Dim eleRow As MSHTML.IHTMLElement 'Row elements
  Dim eleCol As MSHTML.IHTMLElement 'Column elements
  
  Set eleColtr = HTMLDoc.getElementsByTagName("tr") 'Find all tr tags
  'This section populates Excel
      i = 0 'start with first value in tr collection
      For Each eleRow In eleColtr 'for each element in the tr collection
          Set eleColtd = HTMLDoc.getElementsByTagName("tr")(i).getElementsByTagName("td") 'get all the td elements in that specific tr
          j = 0 'start with the first value in the td collection
          For Each eleCol In eleColtd 'for each element in the td collection
              Sheets("Sheet1").Range("A1").Offset(i, j).Value = eleCol.innerText 'paste the inner text of the td element, and offset at the same time
              j = j + 1 'move to next element in td collection
          Next eleCol 'rinse and repeat
          i = i + 1 'move to next element in td collection
      Next eleRow 'rinse and repeat
  
  
  
  Debug.Print "DONE!"
  Exit Sub
  Err_Clear:
  If Err &lt;&gt; 0 Then
  Err.Clear
  Resume Next
  End If
  
  
  End Sub
  </code></pre>