Su314
Su314

Reputation: 59

Wait until download bar appears before continuing code

I am trying to automate downloading data from a website. What I am currently trying to do is wait until the download pop up shown below appears before pressing alt+S: DownloadPopup Once this is done, I want vba to wait until the download complete popup appears before continuing the process DownloadCompletePopup

So far I have tried the code below to try to identify and wait for the download bar, however both FindInitDownloadPopup() and FindDownloadPopup2() do not exit the loop even when the download bar appears. I have looked at other similar posts however none of the propose solutions worked for me

Option Explicit

Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, 
ByVal lpWindowName As String) As Long

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 FindInitDownloadPopup()
Dim ie As InternetExplorer
Dim h As Long
h = ie.hWnd
h = FindWindowEx(h, 0, "Frame Notification Bar", vbNullString)

If h = 0 Then
    Do While h = 0
        Application.Wait (Now + TimeValue("00:00:02"))
        h = FindWindowEx(h, 0, "Frame Notification Bar", vbNullString)
    Loop
Else
End If
End Sub

Sub FindDownloadPopup2()
Dim hWnd As Long
Do
    hWnd = FindWindow("#32770", "File Download")
    DoEvents
Loop Until hWnd
End Sub

Upvotes: 1

Views: 201

Answers (1)

alowflyingpig
alowflyingpig

Reputation: 738

I had a similar problem. My solution was to clear the downloads folder prior to running the code and then I created a simple loop looking for the completed download..

Code for the "Click save and wait for download" is here. Note I use Sleep as I found that for me I needed to wait due to connection issues with my internal intranet. Also I run this code based on ClaimNumber (I work in Finance) thus ommit if needed

' Declare Sleep
#If VBA7 Then
    Public Declare PtrSafe Sub Sleep Lib "kernel32" _
            (ByVal dwMilliseconds As LongPtr)    ' For 64 Bit Systems
#Else
    Public Declare Sub Sleep Lib "kernel32" _
            (ByVal dwMilliseconds As Long)    ' For 32 Bit Systems
#End If

Private Sub ClickSave(ClaimNumber As String)

    Sleep 1000

    Dim o As IUIAutomation
    Set o = New CUIAutomation

    Do
        Dim h As Long
        h = IE.HWND
        h = FindWindowEx(h, 0, "Frame Notification Bar", vbNullString)

        If h <> 0 Then
            Dim count As Long
            count = 0
            Exit Do
        Else
            Sleep 100
            count = count + 1
            If count = 50 Then Exit Sub
        End If
    Loop

    Dim e As IUIAutomationElement
    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)

    Do
        On Error Resume Next
        Dim InvokePattern As IUIAutomationInvokePattern
        Set InvokePattern = Button.GetCurrentPattern(UIA_InvokePatternId)

        If Err.Number = 0 Then
            On Error GoTo 0
            Exit Do
        Else
            Sleep 100
            count = count + 1
        End If
        On Error GoTo 0
    Loop Until count = 100

    InvokePattern.Invoke

    Do
        Sleep 1000
        Completed = DownloadComplete(ClaimNumber)
        If Completed = "Yes" Then Exit Do
    Loop

    SendMessage h, WM_CLOSE, 0, 0


End Sub

Private Function DownloadComplete(ClaimNumber As String) As String

    Dim FSO As FileSystemObject
    Set FSO = New FileSystemObject

    Dim Username As String
    Username = Environ("username")

    Dim DownloadFolder As String
    DownloadFolder = "C:\Users\" & Username & "\Downloads"

    Debug.Print DownloadFolder

    Dim Folder As Scripting.Folder
    Set Folder = FSO.GetFolder(DownloadFolder)

    On Error Resume Next
    Dim File As Scripting.File
    For Each File In Folder.Files
        If File.name Like "*" & ClaimNumber & "*" Then
            If Err.Number = 0 Then
                Completed = "Yes"
            Else
                Completed = "No"
            End If
            On Error GoTo 0
        End If
    Next File

    DownloadComplete = Completed

End Function

Upvotes: 1

Related Questions