title: Using VBA to navigate web page/ maniuplate IE via Access
body: <p>Hello StackOverflow community,</p>
  
  <p>I have a question about using Access VBA to manipulate IE.</p>
  
  <p>Essentially, I am trying to write code that will open a specific webpage using IE, search that page for a particular link (the name of the target link will depend on the circumstances of the user), navigate to a new page by programmatically clicking that link, and then repeating the process by looking for a specific link/element on the resulting new page.</p>
  
  <p>The display text for the ultimate target link will always be the same, but the page that it resides on will be different in every case.</p>
  
  <p>My issue is with programmatically searching the second-level page for the elements that reside there...my results keep giving me only elements from the first-level page, even after the browser has loaded the new link.</p>
  
  <p>Apologies if I'm doing a poor job describing the context of the question so far.</p>
  
  <p>My code, essentially, is as follows:</p>
  
  <pre><code>Dim ie As Object, ieDoc As HTMLDocument
  
  Set ie = CreateObject("InternetExplorer.Application")
  ie.Visible = True
  
  strHTML = "http://targetsite.com/first-level_page"
  
  ie.navigate strHTML
  
  'wait for browser
  While ie.ReadyState &lt;&gt; READYSTATE_COMPLETE
  DoEvents
  Wend
  
  
  'define first-level target link and search for it and then "click" it
  
  Dim i As Integer
  Dim txt As String, link As String, p As String
  
  Set ieDoc = ie.Document
  
  txt = "First-Level Target"
      Do Until link = txt Or i &gt;= 1000
      i = i + 1
      link = ieDoc.Links(i).outerText
  Loop
  
  'I know the above loop is not exactly ideal in its current form, but it does give me a working first attempt at the functionality I'm trying to build.    
  
  ieDoc.Links(i).Click
  </code></pre>
  
  <p>So far, so good. The code above works just as intended. It navigates correctly to the desired second-level page in all cases. Where I'm going wrong is when I try to search that second-level page for the final target element:</p>
  
  <pre><code>'wait for browser
  While ie.ReadyState &lt;&gt; READYSTATE_COMPLETE
  DoEvents
  Wend
  
  'Search for final target element (which always has the same name and anchor text on all second-level pages)
  ieDoc.getElementsByName("final-target-name").Item.Click
  </code></pre>
  
  <p>The line above (based on .getElementsByName) works just fine if I try to use it on the first-level page. But it does nothing once I've navigated to any second-level page. I've also tried to replace it with a modified version of the loop above that searches for the link on the first-level page. Same result.</p>
  
  <p>As a troubleshooting step, I've also replaced that line with a debug.print command to simply name all of the elements on the second-level page, and this always returns the element names from the first page instead.</p>
  
  <p>I assume it's clear that I'm somehow failing to correctly update or redefine my HTMLdocument after I've followed the first link. I've setting ieDoc as nothing and then re-setting it as ie.document, but that doesn't work either.</p>
  
  <p>Hopefully there is a simple command or syntax that I'm simply ignorant to. But for the life of me I havent been able to have VBA correctly refer to the HTML elements existing on the page after the first link is clicked.</p>
  
  <p>Thanks in advance for any advice!</p>
  
  <p>~JQN</p>