O código de automação da Internet VBA não funciona quando ie.Visible = False
Bom dia, estou lutando para encontrar informações sobre um problema que parece não ter muita informação disponível na internet - que é a "barra de notificação de quadros" no internet explorer (a pequena janela amarela que pergunta se você deseja "salvar" ou "abra" um arquivo baixado).
Vou direto ao ponto, o problema que estou tendo é que meu código funciona quando uma visibilidade do Internet Explorer está definida como verdadeira, mas não funciona quando a visibilidade está definida como falsa. Passei pelo código nas duas situações para ver o que muda e notei que o identificador da barra de notificação do quadro altera o valor, mas, além disso, todos são iguais. O código relevante é:
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
Sub StartIE()
Dim appIE As Object
Dim URLString As String
Dim HTMLdoc, btn As Object
Set appIE = CreateObject("internetexplorer.application") ' create an instance of internet explorer
With appIE
.Navigate "https://analytics.twitter.com/user" 'this url wont work for you. you will need to have your own twitter account on twitter analytics, and copy the link to the "tweets" page
.Visible = True ' and show the IE
End With
Do While appIE.Busy Or (appIE.READYSTATE <> 4) ' wait until IE has finished loading
DoEvents
Loop
URLString = appIE.LocationURL
Set HTMLdoc = appIE.document
Set btn = HTMLdoc.getElementsByClassName("btn btn-default ladda-button")(0) 'finds the export data button
btn.Click
Do While appIE.Busy Or (appIE.READYSTATE <> 4) ' wait until IE has finished loading
DoEvents
Loop
Application.Wait (Now + TimeValue("0:00:07"))
Dim hwnd As LongPtr, h As LongPtr
Dim o As IUIAutomation ' The following steps are used to download a csv file from a webpage
Dim e As IUIAutomationElement
Set o = New CUIAutomation
h = appIE.hwnd
h = FindWindowEx(h, 0, "Frame Notification Bar", vbNullString) ' we must find the first frame notification handle
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
h = appIE.hwnd
h = FindWindowEx(h, 0, "Frame Notification Bar", vbNullString)
If h = 0 Then Exit Sub
Set e = o.ElementFromHandle(ByVal h)
Dim iCnd2 As IUIAutomationCondition
Set iCnd2 = o.CreatePropertyCondition(UIA_NamePropertyId, "Open") ' similar to the above snippet, except for the second stage of the frame notification window
Dim Button2 As IUIAutomationElement
Set Button2 = e.FindFirst(TreeScope_Subtree, iCnd2)
Dim InvokePattern2 As IUIAutomationInvokePattern
Set InvokePattern2 = Button2.GetCurrentPattern(UIA_InvokePatternId)
InvokePattern2.Invoke
End Sub
Fora deste código, o trecho em que acredito que o problema está ocorrendo está em:
Dim o As IUIAutomation ' The following steps are used to download a csv file from a webpage
Dim e As IUIAutomationElement
Set o = New CUIAutomation
h = appIE.hwnd
h = FindWindowEx(h, 0, "Frame Notification Bar", vbNullString) ' we must find the first frame notification handle
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
Alguém pode me fornecer uma idéia de por que isso pode estar acontecendo e como eu posso corrigi-lo? Eu sei que já estou pedindo muito, mas eu realmente adoraria uma explicação juntamente com qualquer correção, pois estou tentando melhorar meu entendimento e acho que será benéfico para outras pessoas em uma situação semelhante :)
Agradeço antecipadamente.