title: Press Save Button IE Download Prompt via Custom Functions
body: <p>I have managed to erase all sendkeys from my sub to pull a spreadsheet from IE except for hitting the &quot;save&quot; button on the download prompt at the end. I know there are many posts about this, and I have read them all, but today I found what looks like a great function, but I can't seem to adapt it/get it to work.</p>
  <p>Note: This is not a URL I can call w/ Params, but I enter information on the page and hit button <code>Excel</code> which then spits back a download prompt for a spreadsheet.</p>
  <p>I'm also not sure if the below code is meant to run on the &quot;Download&quot; full window (CTRL + J) or the prompt bar down at the bottom, but I tried both without luck. I can open the &quot;full window&quot; if needed, but it would require sendkeys which I'm looking to eliminate, but currently the code naturally just displays the download &quot;bar&quot; at the bottom of the window.</p>
  <p>Here is some links:</p>
  <p><a href="https://stackoverflow.com/questions/56063122/excel-vba-to-save-as-from-ie-11-download-bar/56063693#comment117157689_56063693">Excel VBA to Save As from IE 11 Download Bar</a></p>
  <p><a href="https://www.mrexcel.com/board/threads/need-help-regarding-ie-automation-using-vba.502298/page-2" rel="nofollow noreferrer">https://www.mrexcel.com/board/threads/need-help-regarding-ie-automation-using-vba.502298/page-2</a></p>
  <p>Here is my code so far w/ two variations using each link:</p>
  <p>Functions (separate module)</p>
  <pre><code>#If VBA7 Then
  'Code is running VBA7 (2010 or later).
  
       #If Win64 Then
       'Code is running in 64-bit version of Microsoft Office.
        Private Declare PtrSafe Function OpenClipboard Lib &quot;user32&quot; (ByVal hWnd As Long) As Long
        Private Declare PtrSafe Function CloseClipboard Lib &quot;user32&quot; () As Long
        Public Declare PtrSafe Function SetForegroundWindow Lib &quot;user32&quot; (ByVal hWnd As Long) As Long
        Private Declare PtrSafe Function ShowWindow Lib &quot;user32&quot; (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long
        Private Declare PtrSafe Function IsIconic Lib &quot;user32.dll&quot; (ByVal hWnd As Long) As Long
       #Else
       'Code is running in 32-bit version of Microsoft Office.
        Private Declare Function OpenClipboard Lib &quot;user32&quot; (ByVal hWnd As Long) As Long
        Private Declare Function CloseClipboard Lib &quot;user32&quot; () As Long
        Public Declare Function SetForegroundWindow Lib &quot;user32&quot; (ByVal hWnd As Long) As Long
        Private Declare Function ShowWindow Lib &quot;user32&quot; (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long
        Private Declare Function IsIconic Lib &quot;user32.dll&quot; (ByVal hWnd As Long) As Long
       #End If
  
  #Else
  'Code is running VBA6 (2007 or earlier).
  
  #End If
  Public IEObj As Object
  Public Sub Activate_A_Window(WindowName As String)
      Dim IE As Object
      Dim Windows As Object: Set Windows = CreateObject(&quot;Shell.Application&quot;).Windows
      Dim Window As Object
      Dim my_title As String
  
      For Each Window In Windows
          my_title = Window.LocationName
          Debug.Print &quot;Window Title = &quot; &amp; my_title
          If InStr(1, my_title, WindowName) Then
              Set IE = Window
              Exit For
          End If
      Next Window
  
      If Not IE Is Nothing Then 'Make sure IE was found as a window
          If CBool(IsIconic(IE.hWnd)) Then ' If it's minimized, show it
              ShowWindow IE.hWnd, SW_RESTORE
          End If
  
          SetForegroundWindow IE.hWnd 'Set the window as the foreground
      Else
          Debug.Print (WindowName &amp; &quot; could not be located&quot;)
      End If
  
  End Sub
  Public Sub OpenIEURL(URL As String, Optional SecondURL As String)
   Application.Wait (Now() + TimeValue(&quot;00:00:01&quot;))
   Set IEObj = CreateObject(&quot;InternetExplorer.Application&quot;)
      
   IEObj.navigate URL
   Do Until IEObj.readyState = 4
    DoEvents
   Loop
   IEObj.TheaterMode = True
   IEObj.Visible = True
   
   If SecondURL &lt;&gt; &quot;&quot; Then
    IEObj.navigate SecondURL
   End If
   
   'Bring IEObj to Focus
   HWNDSrc = IEObj.hWnd
   SetForegroundWindow HWNDSrc
   IEObj.Visible = False
   IEObj.Visible = True
   Call Activate_A_Window(URL)
  End Sub
  Public Sub CloseIEObj()
   'Unload IE
   IEObj.TheaterMode = False
   Application.Wait (Now() + TimeValue(&quot;00:00:04&quot;))
   IEObj.Quit
   Set IEObj = Nothing
   Application.Wait (Now() + TimeValue(&quot;00:00:01&quot;))
  End Sub
  </code></pre>
  <p>Module:</p>
  <pre><code>Option Explicit
  Public Declare Sub Sleep Lib &quot;kernel32&quot; _
      (ByVal dwMilliseconds As Long)
  Public Declare Function FindWindow Lib &quot;user32&quot; Alias &quot;FindWindowA&quot; _
      (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
  Public Declare Function FindWindowEx Lib &quot;user32&quot; Alias &quot;FindWindowExA&quot; _
      (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
  Public Declare Function SetForegroundWindow Lib &quot;user32&quot; _
      (ByVal hWnd As Long) As Long
  Public Declare Function SendMessage Lib &quot;user32&quot; Alias &quot;SendMessageA&quot; _
      (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
  Public Declare Function SendMessageByString Lib &quot;user32&quot; Alias &quot;SendMessageA&quot; _
      (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
  Public Const BM_CLICK = &amp;HF5
  Public Const WM_SETTEXT = &amp;HC
  Public Const WM_GETTEXT = &amp;HD
  Public Const WM_GETTEXTLENGTH = &amp;HE
  
  Private Sub File_Download_Click_Save()
      
      Dim hWnd As Long
      Dim timeout As Date
      
      Debug.Print &quot;File_Download_Click_Save&quot;
      
      'Find the File Download window, waiting a maximum of 30 seconds for it to appear
      
      timeout = Now + TimeValue(&quot;00:00:30&quot;)
      Do
          hWnd = FindWindow(&quot;#32770&quot;, &quot;File Download&quot;)
          DoEvents
          Sleep 200
      Loop Until hWnd Or Now &gt; timeout
      
      Debug.Print &quot;   File Download window &quot;; Hex(hWnd)
      
      If hWnd Then
          'Find the child Save button
          
          hWnd = FindWindowEx(hWnd, 0, &quot;Button&quot;, &quot;&amp;Save&quot;)
          Debug.Print &quot;   Save button &quot;; Hex(hWnd)
      End If
      
      If hWnd Then
      
          'Click the Save button
          
          SetForegroundWindow (hWnd)
          Sleep 600  'this sleep is required and 600 miiliseconds seems to be the minimum that works
          SendMessage hWnd, BM_CLICK, 0, 0
      End If
  End Sub
  
  Public Sub WVinput()
   Dim URL As String
   URL = &quot;http://url.com&quot;
   Call OpenIEURL(URL)
   
      IEObj.document.getElementById(&quot;buttonExcel&quot;).Click ' Hit Excel Button
      Application.Wait (Now() + TimeValue(&quot;00:00:10&quot;))
  
      Call File_Download_Click_Save ' This didn't work
  
  'wait for save as window to appear
  Dim o As IUIAutomation
  Dim h As LongPtr
  Set o = New CUIAutomation
  h = 0
  Do Until h &gt; 0
      'h = ie.hWnd
      h = FindWindow(&quot;#32770&quot;, &quot;Internet Explorer&quot;)
  Loop
  
  'find and click save as button
  Dim e As IUIAutomationElement
  Dim iCnd As IUIAutomationCondition
  Dim Button As IUIAutomationElement
  Set e = o.ElementFromHandle(ByVal h)
  Set Button = Nothing
  Do Until Not Button Is Nothing
      'Set iCnd = o.CreatePropertyCondition(UIA_NamePropertyId, &quot;Save as&quot;)
      Set iCnd = o.CreatePropertyCondition(UIA_NamePropertyId, &quot;Save&quot;) ' Note I'm trying to hit &quot;Save&quot; not &quot;Save As&quot;
      Set Button = e.FindFirst(TreeScope_Subtree, iCnd)
  Loop
  
  Dim InvokePattern As IUIAutomationInvokePattern
  Set InvokePattern = Button.GetCurrentPattern(UIA_InvokePatternId)
  InvokePattern.Invoke
  
      'SendKeys old method
      'SendKeys &quot;%s&quot;, True ' Select &quot;Save&quot; in DL Window
      'Application.Wait (Now() + TimeValue(&quot;00:00:02&quot;))
      'SendKeys &quot;%s&quot;, True ' Select &quot;Save&quot; in DL Window
      Application.Wait (Now() + TimeValue(&quot;00:00:02&quot;))
      
   Call CloseIEObj
   
  End Sub
  </code></pre>