VBA & IE automation - The unkown window!

Bullstrik1

Board Regular
Joined
Jul 31, 2014
Messages
66
Hi everyone!

I'm trying to automate a task where i mustacess this website and download 2 files.
I managed to get to the part where i have to click on a "button" to download the file, however the message that IE sends to deal with the file is somewhat out of the comon:

45HZzuv.png


Seems like this window has no name (confirmed to me by my IT colegues that helped me out the other day trying to figure out how in the heck i would call this thing :P ) and so,it poses a problem when i try to use FindWindowEx API function...

Anyway, the code i got so far is this:

Code:
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 GALP_MP_Download()


'                           MP
'
'   Macro construída por Sérgio Torrinha 08-08-2015
'   Código para abrir website e introduzir credênciais GALP
'
'




'to refer to the running copy of Internet Explorer
Dim ie As InternetExplorer, UserDldD As String, doc As HTMLDocument
Dim h As LongLong, o As IUIAutomation, e As IUIAutomationElement, iCnd As IUIAutomationCondition, Button As IUIAutomationElement, InvokePattern As IUIAutomationInvokePattern




    '   define directoria de downloads do user
UserDldD = "C:\Users\" & Environ("UserName") & "\Downloads\"


'open Internet Explorer in memory, and go to website
Set ie = New InternetExplorer


With ie
        .Visible = True ' Podemos optar por nao mostrar a janela do browser o que irá permitir acelerar todo o processo. Para tal basta escolher a opção "FALSE"
        .navigate ("https://galplogin.galpenergia.com/GalpLogin/login.aspx?ReturnUrl=http://galpfrota.galpenergia.com/Comum/COM_Login.asp?GLPLogin=Yes")
        While .Busy Or .readyState <> 4: DoEvents: Wend
            
                ' Código para fazer log in no site Via Verde através do InternetExplorer
    With .document
        .getElementById("UserName").Value = "USERNAME"
        .getElementById("Password").Value = "PASSWORD"
        .getElementById("LoginLinkButton").Click
    End With
    
    While .Busy Or .readyState <> 4: DoEvents: Wend
        
    .navigate "http://galpfrota.galpenergia.com/COMUM_S/COM_DOWNLOAD.ASP"
    
    While .Busy Or .readyState <> 4: DoEvents: Wend
    'Application.Wait (Now() + TimeValue("00:00:05"))
    
End With




    Set doc = ie.document
        
    With doc
          Dim Form As HTMLFormElement, DivTag As IHTMLElement, DivTag1 As IHTMLElement, Cbx As IHTMLElement, Cbx1 As IHTMLElement, Cbx2 As IHTMLElement
          Dim Tbl As IHTMLElement, Tbl1 As IHTMLElement, OptionX As IHTMLElement, td As IHTMLElement, a As IHTMLElement
          
          ' Define form a submeter
          
          Set Form = .forms("Form_GFO")
          
          With Form
                
                '******************************************************************************'
                '                                                                              '
                '                             DOWNLOAD FICHEIROS TXT                           '
                '                                                                              '
                '******************************************************************************'
                
                  '   Define primeira combobox (onde escolhemos a data da factura) e escolhe a data da factura que pretendemos
                  For Each DivTag In .getElementsByTagName("Div")
                      If DivTag.ID = "second" Then
                          Set DivTag1 = DivTag
                          Exit For
                      End If
                  Next DivTag
                  
                  With DivTag1
                      For Each Cbx In .getElementsByTagName("select")
                          If Cbx.ID = "ddlPeriodoFactTXT" Then
                              Set Cbx1 = Cbx
                              Exit For
                          End If
                      Next Cbx
                      Cbx1.Value = "31/07/2015"
                  End With
                  
                  '   define a segunda combobox e escolhe o tipo de informação que pretendemos fazer download
                  .getElementsByTagName("select")("ddlTipo").Value = "TXT"
                  
                  For Each Tbl In .getElementsByTagName("table")
                      If Tbl.ID = "tblDownloadFile" Then
                          Set Tbl1 = Tbl
                          Exit For
                      End If
                  Next
                  
                  '   Eliminina todos os ficheiros na directoria de downloads
                  On Error Resume Next
                  Kill UserDldD & "*.*"
                  On Error GoTo 0
                  
                  Application.Wait (Now() + TimeValue("0:00:05"))
                  
                  .submit
            
            End With
            
            '   Clica no Link para download dos ficheiros TxT
            
            Call .parentWindow.execScript("DownloadFile('TXT')", "JavaScript")


            Application.Wait (Now() + TimeValue("0:00:05"))
            
            Set o = New CUIAutomation


            h = ie.hwnd
            h = FindWindowEx(h, 0, "Internet Explorer", vbNullString) '<---- here is where i have problems. The window has no name


            If h = 0 Then Exit Sub


            Set e = o.ElementFromHandle(ByVal h)


            Set iCnd = o.CreatePropertyCondition(UIA_NamePropertyId, "Save")


            Set Button = e.FindFirst(TreeScope_Subtree, iCnd)


            Set InvokePattern = Button.GetCurrentPattern(UIA_InvokePatternId)


            InvokePattern.Invoke
'
            
            Application.Wait (Now() + TimeValue("0:00:05"))
            
          '******************************************************************************'
          '                                                                              '
          '                             DOWNLOAD FICHEIROS XLS                           '
          '                                                                              '
          '******************************************************************************'
          
          
          
        End With
    
Label_Exitsub:
ie.Quit
Set ie = Nothing


End Sub

Pls note this code is not complete as i was developing it and i came across this problem and still no solution to it so far.
So, any thoughts on this one? I'm kinda stumped...i even try to set focus on IE and use SENDKEYS method (wich i alrdy know its not good to use, and i don't want to use it !) and i got no success because when this window pops-up seems like i can focus the principal IE window....

Tkx all in advance :)
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
looks to me that IE dosen't know what the default should be with a zip file, hence asking
 
Upvote 0
looks to me that IE dosen't know what the default should be with a zip file, hence asking

yeah that was what my IT colegues thought last Friday when i got stumped with this. We redefined the way how my machine opens Zip files and it didnt fix the problem.

Any thoughts?
 
Upvote 0

Forum statistics

Threads
1,213,536
Messages
6,114,205
Members
448,554
Latest member
Gleisner2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top