title: Downloading File from IE using VBA
body: <p>I am currently working on a VBA code that retrieves the top file from this website  (<a href="http://infopost.bwpmlp.com/Posting/default.aspx?Mode=Display&amp;Id=27&amp;tspid=100000" rel="nofollow">http://infopost.bwpmlp.com/Posting/default.aspx?Mode=Display&amp;Id=27&amp;tspid=100000</a>). I am able to click on the button using Javascript in my code, and I am able to click on open after the download is kicked off. However, I am having trouble saving the file. Because the workbook is being pulled from a website there really isn't a way to set it to the active workbook that I can think of. Currently when I do ActiveWorkbook.SaveAs the code is saving the blank workbook that I am testing the code out of. The file I downloaded seems to not open until the entire code is done running even after I try putting in breaks. Anyone have any ideas? My code is below. Thanks!</p>
  
  <pre><code> Option Explicit
  Dim ie As InternetExplorer
  Dim h As LongPtr
  Private Declare PtrSafe Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As LongPtr, ByVal hWnd2 As LongPtr, ByVal lpsz1 As String, ByVal lpsz2 As String) As LongPtr
  
  
  Sub Texas_Gas()
  Application.DisplayAlerts = True
  
  Dim ie As Object
  Dim IeHandle As Long, FileDownloadHandle As Long, OpenButtonHandle As Long, IePopupBarHandle As Long
  Dim AutoMode As Boolean, FileDownloadClassicPopup As Boolean, DownloadComplete As Boolean
  Dim Timeout As Date
  Dim strSPICE As String, strLink As String
  Dim PopupGap As Integer, i As Integer
  Set ie = CreateObject("InternetExplorer.Application")
  
  DownloadComplete = False
      FileDownloadClassicPopup = False
      FileDownloadHandle = 0
      IePopupBarHandle = 0
  
  
  With ie
  .Visible = True
  .navigate "http://infopost.bwpmlp.com/Posting/default.aspx?    Mode=Display&amp;Id=27&amp;tspid=100000"
      Do While .Busy: DoEvents: Loop
      Do While .readyState &lt;&gt; 4: DoEvents: Loop
  End With
  
  ie.document.parentWindow.execScript "javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(""dgITMatrix:0:lnkBtnDownload"", """", true, """", """", false, true))"
  
  
  Dim o As IUIAutomation
  Dim e As IUIAutomationElement
  Dim iCnd As IUIAutomationCondition
  Set o = New CUIAutomation
  h = ie.Hwnd
  h = FindWindowEx(h, 0, "Frame Notification Bar", vbNullString)
  
  
  If h = 0 Then Exit Sub
  
  Set e = o.ElementFromHandle(ByVal h)
  
  Set iCnd = o.CreatePropertyCondition(UIA_NamePropertyId, "Open")
  
  Dim Button As IUIAutomationElement
  Set Button = e.FindFirst(TreeScope_Subtree, iCnd)
  Dim InvokePattern As IUIAutomationInvokePattern
  Set InvokePattern = Button.GetCurrentPattern(UIA_InvokePatternId)
  InvokePattern.Invoke
  
  
  ActiveWorkbook.SaveAs "I:\Cap_Rel\raw_scrapes\Texas_Gas_Transmission\parsed\Texas_Gas_Transmission_CapRel" &amp; Format(Date - 1, "yyyymmdd") &amp; "MACRO", FileFormat:=xlCSV
  
  End Sub
  </code></pre>