how can I exclude the Line Application.Wait (Now + TimeValue("0:00:20"))
and let excel automatically detect the download bar as the waiting time is not Fixed.
and how can I Press on this Image directly without looping all imgs element.getAttribute("src") = "/xmlpserver/theme/al_excel.gif
Please ignore if you found a lot of declaration as is put them for testing purposes.
Public Declare Function FindWindowEx Lib "user32.dll" Alias "FindWindowExA" ( _
ByVal hwndParent As Long, _
ByVal hwndChildAfter As Long, _
ByVal lpszClass As String, _
ByVal lpszWindow As String) As Long
Sub Test()
Const cURL = "https://mywebsite"
Const cUsername = "xxxxxx" 'REPLACE XXXX WITH YOUR USER NAME
Const cPassword = "xxxxxxxxxxx" 'REPLACE YYYY WITH YOUR PASSWORD
Dim ie As InternetExplorerMedium
Dim doc As htmlDocument
Dim LoginForm As HTMLFormElement
Dim CIFs As MSHTML.HTMLWindow2
Dim CIFBOX As HTMLInputElement
Dim UserNameInputBox As HTMLInputElement
Dim PasswordInputBox As HTMLInputElement
Dim SignInButton As HTMLInputButtonElement
Dim element As Object
Dim htmlDoc As htmlDocument
Dim HTMLelement As IHTMLElement
Dim ieEvent
Set ie = New InternetExplorerMedium
With ie
ie.Visible = True
ie.navigate cURL
Do While ie.readyState <> READYSTATE_COMPLETE Or ie.Busy: DoEvents: Loop
Set doc = ie.document
On Error Resume Next
doc.getElementsByName("overridelink").Item.Click
Application.Wait (Now + TimeValue("0:00:03"))
Set LoginForm = doc.forms(0)
Set UserNameInputBox = LoginForm.elements("j_username")
UserNameInputBox.Value = cUsername
'Get the password textbox and populate it
Set PasswordInputBox = LoginForm.elements("j_Password")
PasswordInputBox.Value = cPassword
'Get the form input button and click it
Set SignInButton = LoginForm.elements("btn_login")
SignInButton.Click
'Wait for the new page to load
Do While ie.readyState <> READYSTATE_COMPLETE Or ie.Busy: DoEvents: Loop
ie.navigate "https:/mywebsite2"
Do While ie.readyState <> READYSTATE_COMPLETE Or ie.Busy: DoEvents: Loop
Dim img As MSHTML.HTMLImg
Set doc = ie.document
Set CIFs = doc.frames(0)
CIFs.document.getElementById("_paramsCIF_NO").Value = Range("A3")
Set AllInputs = CIFs.document.getElementsByTagName("img")
For Each element In AllInputs
If element.getAttribute("src") = "/xmlpserver/theme/al_excel.gif" Then
element.Click
Exit For
End If
Next element
Application.Wait (Now + TimeValue("0:00:20"))
AutoSave
MsgBox ("Screen Update Done")
End With
End Sub
Public Function AutoSave() As Boolean
On Error GoTo handler
Dim sysAuto As New UIAutomationClient.CUIAutomation
Dim ieWindow As UIAutomationClient.IUIAutomationElement
Dim cond As IUIAutomationCondition
Set cond = sysAuto.CreateAndCondition(sysAuto.CreatePropertyCondition(UIA_NamePropertyId, "Notification"), _
sysAuto.CreatePropertyCondition(UIA_PropertyIds.UIA_ControlTypePropertyId, UIA_ToolBarControlTypeId))
Set ieWindow = sysAuto.GetRootElement.FindFirst(TreeScope_Descendants, cond)
Dim tField As UIAutomationClient.IUIAutomationElement
Dim tFieldCond As IUIAutomationCondition
Set tFieldCond = sysAuto.CreatePropertyCondition(UIA_ControlTypePropertyId, UIA_ControlTypeIds.UIA_SplitButtonControlTypeId)
Set tField = ieWindow.FindFirst(TreeScope_Descendants, tFieldCond)
Dim invPattern As UIAutomationClient.IUIAutomationInvokePattern
Set invPattern = tField.GetCurrentPattern(UIA_InvokePatternId)
invPattern.Invoke
AutoSave = True
Exit Function
handler:
End Function