pmr
pmr

Reputation: 1006

Controlling IE11 "Do you want to Open/Save" dialogue window buttons in VBA

We need to download file from a NASDAQ website automatically. My existing VBA code is opening an IE "Do you want to Open/Save" dialogue window. How to click on that save button and give a path via VBA ? I have tried various windows api methods described in this link here also but that is giving a result of "Window Not Found".

My current code is as below:

Sub MyIEauto()

    Dim ieApp As InternetExplorer
    Dim ieDoc As Object
    'Dim ieTable As Object

    'create a new instance of ie
    Set ieApp = New InternetExplorer

    'you don’t need this, but it’s good for debugging
    ieApp.Visible = True
    'assume we’re not logged in and just go directly to the login page
    ieApp.Navigate "https://indexes.nasdaqomx.com/Account/LogOn"
    Do While ieApp.Busy: DoEvents: Loop
    Do Until ieApp.readyState = READYSTATE_COMPLETE: DoEvents: Loop

    Set ieDoc = ieApp.Document
    'fill in the login form – View Source from your browser to get the control names
    With ieDoc.forms(0)
        .UserName.Value = "xxxxxxx"
        .Password.Value = "xxxxxxx"
        .submit
    End With
    Do While ieApp.Busy: DoEvents: Loop
    Do Until ieApp.readyState = READYSTATE_COMPLETE: DoEvents: Loop

    'now that we’re in, go to the page we want
    ieApp.Navigate "https://indexes.nasdaqomx.com/Index/ExportWeightings/NDX?tradeDate=2015-08-19T00:00:00.000&timeOfDay=SOD/SODWeightings_2015"

    'next below line commented as it is failing
    'ieApp.ExecWB 4, 2, "D:\VBA code work\SODWeightings_20150819_NDX.xlsx" 

    set ieApp=Nothing
    set ieDoc=Nothing

End Sub

The screenshot below shows where I have reached. How do I progress from here?

enter image description here

Upvotes: 6

Views: 51832

Answers (4)

Johan de Wispelaere
Johan de Wispelaere

Reputation: 21

as ieApp.hWnd in a 64bit environment is LongLong, where h is Long this yields a Type Mismatch which can easily been solved by

h = Clng(ieApp.hWnd)

Upvotes: 2

Flash
Flash

Reputation: 1

SendKeys was the solution for me.

myfile = "C:\Users\User\Downloads\myfile.xls" 
checkmyfile = Dir(myfile, vbArchive)

Do While checkmyfile = ""
    On Error Resume Next
    checkmyfile = Dir(myfile , vbArchive)
    If checkmyfile = "myfile.xls" Then Exit Do 
    AppActivate "Title - Internet Explorer"
    SendKeys "%(g)"
    Application.Wait Now + TimeValue("0:0:1")
Loop

Upvotes: 0

Tony L.
Tony L.

Reputation: 19396

Another way to do this is to send the keystrokes of the shortcut keys to click the save button in IE11. I should note your IE window will need to be the active window for this to work. Thus, it won't work while in debug mode.

The code below calls the shortcut key. I'm just showing the shortcut key so you have a better idea what's happening.

  • Shortcut key:Alt+S
  • VBA: Application.SendKeys "%{S}"

Upvotes: 4

pmr
pmr

Reputation: 1006

It's solved finally...

Option Explicit

Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
(ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, _
ByVal lpsz2 As String) As Long

Public Sub AddReference()

    ThisWorkbook.VBProject.References.AddFromFile "C:\Windows\SysWOW64\UIAutomationCore.dll"

End Sub

'after my original code as posted in question then this below lines

Dim o As IUIAutomation
    Dim e As IUIAutomationElement
    Set o = New CUIAutomation
    Dim h As Long
    h = ieApp.hWnd
    h = FindWindowEx(h, 0, "Frame Notification Bar", vbNullString)
    If h = 0 Then Exit Sub

    Set e = o.ElementFromHandle(ByVal h)
    Dim iCnd As IUIAutomationCondition
    Set iCnd = o.CreatePropertyCondition(UIA_NamePropertyId, "Save")

    Dim Button As IUIAutomationElement
    Set Button = e.FindFirst(TreeScope_Subtree, iCnd)
    Dim InvokePattern As IUIAutomationInvokePattern
    Set InvokePattern = Button.GetCurrentPattern(UIA_InvokePatternId)
    InvokePattern.Invoke

Upvotes: 17

Related Questions