'References required:
'Microsoft Internet Controls (for InternetExplorer class)
'Microsoft HTML Object Library (for HTMLDocument and related classes)
'UIAutomation (for IUIAutomation and related classes)
Option Explicit
'https://msdn.microsoft.com/en-us/library/office/gg264421.aspx
'64-Bit Visual Basic for Applications Overview
#If VBA7 Then
'New VBA version 7 compiler, therefore >= Office 2010
'PtrSafe means function works in 32-bit and 64-bit Office
'LongPtr type alias resolves to Long (32 bits) in 32-bit Office, or LongLong (64 bits) in 64-bit Office
Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)
Private Declare PtrSafe Function SetForegroundWindow Lib "user32.dll" (ByVal hwnd As LongPtr) 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
#Else
'Old VBA version 6 or earlier compiler, therefore <= Office 2007
Private Declare Sub Sleep Lib "kernel32" (ByVal milliseconds As Long)
Private Declare Function SetForegroundWindow Lib "user32.dll" (ByVal hwnd As Long) 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
#End If
Public Sub IE_Download_File()
Dim IE As InternetExplorer
Dim HTMLdoc As HTMLDocument
Dim URL As String
URL = "http://bvmf.bmfbovespa.com.br/opcoes/opcoes.aspx?idioma=pt-br"
Set IE = New InternetExplorer
With IE
.Visible = True
.navigate URL
SetForegroundWindow .hwnd
Do While .Busy Or .readyState <> 4: DoEvents: Loop
Set HTMLdoc = .document
End With
'Enter download date from Sheet1 cell A2
'< input name="ctl00$contentPlaceHolderConteudo$posicoesAbertoEmp$txtConsultaDataDownload" id="ctl00_contentPlaceHolderConteudo_posicoesAbertoEmp_txtConsultaDataDownload" readonly="readonly" class="datepicker hasDatepicker" value="10/03/2017" *******="alert('Selecione a data no ícone do calendário ao lado.');" onkeypress="alert('Selecione a data no ícone do calendário ao lado.');" type="text" >
'HTMLdoc.getElementById("ctl00_contentPlaceHolderConteudo_posicoesAbertoEmp_txtConsultaDataDownload").Value = Format(Worksheets("Sheet1").Range("A2").Value, "dd/mm/yyyy")
HTMLdoc.forms(0).Item(12).Value = Format(Worksheets("Sheet1").Range("A2").Value, "dd/mm/yyyy")
'Click Buscar button
'< input name="ctl00$contentPlaceHolderConteudo$posicoesAbertoEmp$btnBuscarArquivos" value="buscar" id="ctl00_contentPlaceHolderConteudo_posicoesAbertoEmp_btnBuscarArquivos" class="button expand" type="submit" >
'HTMLdoc.getElementById("ctl00_contentPlaceHolderConteudo_posicoesAbertoEmp_btnBuscarArquivos").Click
HTMLdoc.forms(0).Item(13).Click
Download_File IE.hwnd
Debug.Print Format(Now, "hh:mm:ss"); " Finished"
End Sub
#If VBA7 Then
Private Sub Download_File(IEhwnd As LongPtr)
#Else
Private Sub Download_File(IEhwnd As Long)
#End If
#If VBA7 Then
Dim hwnd As LongPtr
#Else
Dim hwnd As Long
#End If
Dim UIAuto As IUIAutomation
Dim UIAutoElem As IUIAutomationElement, UIAutoElemButton As IUIAutomationElement, UIAutoElemText As IUIAutomationElement
Dim UIAutoElemToolbar As IUIAutomationElement
Dim UIAutoCond As IUIAutomationCondition, UIAutoCond1 As IUIAutomationCondition, UIAutoCond2 As IUIAutomationCondition
Dim UIAutoInvPatt As IUIAutomationInvokePattern
Dim timeout As Date
Dim notificationBarText As String, p1 As Long, p2 As Long
Dim downloadedFileName As String
Const DebugMode As Boolean = False
'Create UIAutomation object
Set UIAuto = New CUIAutomation
'Find the IE11 Frame Notification bar, waiting a maximum of 10 seconds
timeout = DateAdd("s", 10, Now)
Do
hwnd = FindWindowEx(IEhwnd, 0, "Frame Notification Bar", vbNullString)
DoEvents
Sleep 200
Loop While hwnd = 0 And Now < timeout
If DebugMode Then Debug.Print Format(Now, "hh:mm:ss"); " Frame Notification Bar " & hwnd
If hwnd = 0 Then
MsgBox "IE download Frame Notification bar does not exist"
Exit Sub
End If
'Get UIAutomation element
Set UIAutoElem = UIAuto.ElementFromHandle(ByVal hwnd)
'Create conditions to find the "Save" button (split button type)
'Name = Save
'Class =
'Ctrl type = 50031
'Ctrl name = split button
'Value =
Set UIAutoCond1 = UIAuto.CreatePropertyCondition(UIA_NamePropertyId, "Save") 'Language
Set UIAutoCond2 = UIAuto.CreatePropertyCondition(UIA_ControlTypePropertyId, UIA_SplitButtonControlTypeId)
Set UIAutoCond = UIAuto.CreateAndCondition(UIAutoCond1, UIAutoCond2)
'Find the "Save" button, waiting until it exists
Do
Set UIAutoElemButton = UIAutoElem.FindFirst(TreeScope_Descendants, UIAutoCond)
Sleep 200
If DebugMode Then Debug.Print Format(Now, "hh:mm:ss"); " Find Save"
DoEvents
Loop While UIAutoElemButton Is Nothing
'Click the "Save" button, waiting until it doesn't exist
Do
Set UIAutoInvPatt = UIAutoElemButton.GetCurrentPattern(UIA_InvokePatternId)
On Error Resume Next 'ignore error if Save is clicked and it doesn't exist
UIAutoElemButton.SetFocus
UIAutoInvPatt.Invoke
On Error GoTo 0
If DebugMode Then Debug.Print Format(Now, "hh:mm:ss"); " Save clicked"
Set UIAutoElemButton = UIAutoElem.FindFirst(TreeScope_Descendants, UIAutoCond)
Sleep 200
Loop Until UIAutoElemButton Is Nothing
'Create conditions to find "Notification bar Text"
'Name = Notification bar Text
'Class =
'Ctrl type = 50020
'Ctrl name = Text
'Value = The xxxxxxx.yyy download has completed.
Set UIAutoCond1 = UIAuto.CreatePropertyCondition(UIA_NamePropertyId, "Notification bar Text") 'Language?
Set UIAutoCond2 = UIAuto.CreatePropertyCondition(UIA_ControlTypePropertyId, UIA_TextControlTypeId)
Set UIAutoCond = UIAuto.CreateAndCondition(UIAutoCond1, UIAutoCond2)
'Find the "Notification bar Text" and wait until its Value contains "download has completed"
Do
Set UIAutoElemText = UIAutoElem.FindFirst(TreeScope_Descendants, UIAutoCond)
Sleep 200
DoEvents
notificationBarText = ""
If Not UIAutoElemText Is Nothing Then
notificationBarText = UIAutoElemText.GetCurrentPropertyValue(UIA_LegacyIAccessibleValuePropertyId)
End If
If DebugMode Then Debug.Print Format(Now, "hh:mm:ss"); " "; notificationBarText
Loop Until InStr(notificationBarText, "download has completed")
'Create conditions to find the "Close" button
Set UIAutoCond1 = UIAuto.CreatePropertyCondition(UIA_NamePropertyId, "Close") 'Language
Set UIAutoCond2 = UIAuto.CreatePropertyCondition(UIA_ControlTypePropertyId, UIA_ButtonControlTypeId)
Set UIAutoCond = UIAuto.CreateAndCondition(UIAutoCond1, UIAutoCond2)
'Find the "Close" button, waiting until it exists
Do
Set UIAutoElemButton = UIAutoElem.FindFirst(TreeScope_Descendants, UIAutoCond)
Sleep 200
If DebugMode Then Debug.Print Format(Now, "hh:mm:ss"); " Find Close"
DoEvents
Loop While UIAutoElemButton Is Nothing
'Click the "Close" button
UIAutoElemButton.SetFocus
Set UIAutoInvPatt = UIAutoElemButton.GetCurrentPattern(UIA_InvokePatternId)
UIAutoInvPatt.Invoke
If DebugMode Then Debug.Print Format(Now, "hh:mm:ss"); " Close clicked"
Set UIAutoInvPatt = Nothing
Set UIAutoElemButton = Nothing
Set UIAuto = Nothing
'Extract file name from notification bar text, e.g. "The xxxxxxx.yyy download has completed."
p1 = InStr(notificationBarText, "The ") + 4
p2 = InStr(p1, notificationBarText, " download has completed") 'Language
downloadedFileName = Mid(notificationBarText, p1, p2 - p1)
Debug.Print Format(Now, "hh:mm:ss"); " Downloaded " & downloadedFileName
End Sub