Pause macro till the file downloads

hrayani

Well-known Member
Joined
Jul 23, 2010
Messages
1,501
Office Version
  1. 2016
Platform
  1. Windows
Hello Friends,

I am using this code which download a file & pull some data from it.
VBA Code:
Sub update_database()

Workbooks("REPORTS.xlsm").Worksheets("INDEX").Range("A2").Hyperlinks(1).Follow
    
Workbooks.Open Filename:=Environ$("USERPROFILE") & "\Downloads\DATA ENTRY.xlsm"
    
    Workbooks("DATA ENTRY.xlsm").Worksheets("VALIDATION").Cells.Copy
    Workbooks("REPORTS.xlsm").Worksheets("R VALIDATION").Range("A1").PasteSpecial Paste:=xlPasteValues
        
    Workbooks("DATA ENTRY.xlsm").Close savechanges:=False
    Kill (Environ$("USERPROFILE") & "\Downloads\DATA ENTRY.xlsm")
    Workbooks("REPORTS.xlsm").Save
    
    End Sub

The problem is that sometime it takes the file to download but the code jumps to the next part and give this error

1580116637692.png



Can't we tell the macro to wait until the file is downloaded and then jumps to the workbook open part ??

Regards,

Humayun
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Hi Humayun

You can try check if the file exists. And keep trying until such time that it does exist (i.e. it has downloaded), or a certain amount of time has elapsed. In the example below we keep trying until the file is located or ten seconds has elapsed.

VBA Code:
Sub foo()
    Dim strFile     As String
    Dim blnContinue As Boolean
    Dim dteStart    As Date
    
    Const lngWait   As Long = 10 'wait in seconds
    
    strFile = Environ$("USERPROFILE") & "\Downloads\DATA ENTRY.xlsm"

    dteStart = Now 'log start time
try:
    blnContinue = CBool(Len(Dir(strFile))) 'check if the file exists
    
    If Not blnContinue Then
        If Now <= dteStart + TimeSerial(0, 0, lngWait) Then GoTo try 'try again if the file doesn't exist (i.e. not downloaded)
    End If
    
    If blnContinue Then
        'do what you want to do
        MsgBox "continue"
    Else
        'it isn't downloading
        MsgBox "cannot download file"
    End If

End Sub
 
Upvote 0
On second thoughts I don't like using a label - so in this version I'm opting for a Do Loop:
VBA Code:
Sub foo()
    Dim strFile     As String
    Dim blnContinue As Boolean
    Dim dteStart    As Date
    
    Const lngWait   As Long = 10 'wait in seconds
    
    strFile = Environ$("USERPROFILE") & "\Downloads\DATA ENTRY.xlsm"

    dteStart = Now 'log start time
    
    Do
        blnContinue = CBool(Len(Dir(strFile))) 'check if the file exists
    Loop Until blnContinue Or Now > dteStart + TimeSerial(0, 0, lngWait) 'exit when the file is found or the timer has elapsed
    
    If blnContinue Then
        'do what you want to do
        MsgBox "continue"
    Else
        'it isn't downloading
        MsgBox "cannot download file"
    End If
End Sub
 
Upvote 0
Hi Jon von der Heyden,

Thanks for the solutions.... I am a novice in VBA - still learning from you guys
What I have understood from both the codes is this -

both the codes will exit which ever comes first.
a) file found or b) time out (10 seconds in said case)
Means both the codes are same as far as the functionality is concerned... approach is different
please correct me if I am wrong

Secondaly, as I said that I am a novice. please let me know where to put my part of the code in the above

is it after If blnContinue Then

Code:
If blnContinue Then
        'do what you want to do - my part of the code
        MsgBox "continue"
    Else
        'it isn't downloading
        MsgBox "cannot download file"
    End If
 
Upvote 0
Hi

You are correct that both of my suggestions essentially do the same thing - but I favour the 2nd approach.

Regarding including your code, perhaps something like this (untested) - indeed included within the blnContinue condition:
VBA Code:
Sub foo()
    Dim strFile     As String
    Dim blnContinue As Boolean
    Dim dteStart    As Date
    Dim wkbTarget   As Workbook
    
    Const lngWait   As Long = 10 'wait in seconds
    
    strFile = Environ$("USERPROFILE") & "\Downloads\DATA ENTRY.xlsm"

    dteStart = Now 'log start time
    
    Do
        blnContinue = CBool(Len(Dir(strFile))) 'check if the file exists
    Loop Until blnContinue Or Now > dteStart + TimeSerial(0, 0, lngWait) 'exit when the file is found or the timer has elapsed
    
    If blnContinue Then
        Set wkbTarget = Workbooks.Open(Filename:=strFile) 'set a reference to the workbook
        
        With wkbTarget
            .Worksheets("VALIDATION").Cells.Copy
            Workbooks("REPORTS.xlsm").Worksheets("R VALIDATION").Range("A1").PasteSpecial Paste:=xlPasteValues
            .Close savechanges:=False
        End With
        
        Kill (strFile)
        Workbooks("REPORTS.xlsm").Save
    Else
        'it isn't downloading
        MsgBox "cannot download file"
    End If
End Sub
 
Upvote 0
Hi Jon von der Heyden,

Many thanks....

I will try and keep you posted
 
Upvote 0
Hi hrayani. I'm sure that Jon has offered U the correct resolution but I thought perhaps this bit of code that Jon Peltier provided may also do the trick. HTH. Dave
Code:
Dim t As Double
Workbooks("REPORTS.xlsm").Worksheets("INDEX").Range("A2").Hyperlinks(1).Follow
t = Timer
Do Until Timer - t > 1
  DoEvents
Loop
Workbooks.Open Filename:=Environ$("USERPROFILE") & "\Downloads\DATA ENTRY.xlsm"
 
Upvote 0
Hello NdNoviceHlp,

Thanks very much... I appreciate.

I will for sure try both yours and jon’s code tomorrow as I don’t have access to my computer right now.

Will keep both of you posted

Regards,

Humayun
 
Upvote 0
Just as a FYI - my code essentially does the same as the code from Jon Peltier - with exception that my suggestion incorporates checks as to whether or not the file has downloaded. This way my code will exit possibly sooner than the timer.
 
Upvote 0
Just as a FYI - my code essentially does the same as the code from Jon Peltier - with exception that my suggestion incorporates checks as to whether or not the file has downloaded. This way my code will exit possibly sooner than the timer.

Very well noted... I will share the outcome of yours & Jon Pettier’ code
 
Upvote 0

Forum statistics

Threads
1,215,045
Messages
6,122,830
Members
449,096
Latest member
Erald

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