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:
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:
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
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:
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