title: Excel VBA: auto click and open file from website
body: <p>Thanks to Qharr, I have successfully performed auto search on the website.(My previous question:
  <a href="https://stackoverflow.com/questions/52241366/excel-vba-cannot-perform-auto-search-on-website">Excel VBA: Cannot perform auto search on website</a>)
  I have another question concerning the next step: I would always like to click the first link that appears after clicking the search button, and open the file in order to extract certain data. Is there any ways to do that? Thanks!</p>
  
  <p>Codes that I have at present:</p>
  
  <pre><code>Option Explicit
  Sub Searchstockcode()
  
      Dim SearchString As String, SearchBox As Object, SearchButton As Object, ie As Object
  
      SearchString = "2828"
  
      Set ie = CreateObject("InternetExplorer.Application")
  
      ie.Visible = True
  
      ie.navigate "http://www.hkexnews.hk/listedco/listconews/advancedsearch/search_active_main.aspx"
  
      While ie.Busy Or ie.readyState &lt; 4: DoEvents: Wend
  
      Set SearchBox = ie.document.getElementById("ctl00_txt_stock_code")
      SearchBox.Value = SearchString
  
      Set SearchButton = ie.document.querySelector("[src*='/image/search.gif']")
      SearchButton.Click
  
      While ie.Busy Or ie.readyState &lt; 4: DoEvents: Wend
  
      'Click the first result
      Set TargetFile = ie.document.getElementById("ctl00_gvMain_ctl02_hlTitle") 
      TargetFile.Click
  
      'Here I would like to open the file in excel, but I am stuck at the "save as" pop up.
      'As long as the file can be opened, I should be able to complete the data extraction with my own codes.
  
  ie.Quit
  End Sub
  </code></pre>