Bullstrik1
Board Regular
- Joined
- Jul 31, 2014
- Messages
- 66
Hi everyone!
I'm developing the code below to access a website and download a couple files. I figured today that if i run the code below all goes smothly but if i run the same code all at once IE keeps hanging when it navigates to the second webpage.
Any ideias on how to solve this issue?
I'm having issues in this bit of code:
I'm using IE11 on windows 7.
Tkx in advance for your help!
I'm developing the code below to access a website and download a couple files. I figured today that if i run the code below all goes smothly but if i run the same code all at once IE keeps hanging when it navigates to the second webpage.
Any ideias on how to solve this issue?
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
[COLOR=#333333]End Sub[/COLOR]
I'm having issues in this bit of code:
Code:
[COLOR=#333333][/COLOR] .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
I'm using IE11 on windows 7.
Tkx in advance for your help!