Results 1 to 4 of 4

Thread: Solved: download complete

  1. #1

    Solved: download complete

    Is it possible to determine (via VBA) when a download is complete?

    I'm using the code below to download a file. Currently, I'm using a userform to select the downloaded file to accomplish a task and I would like to eliminate this step in my code to automatically run my code to open the file once the file is downloaded.

    Thanks

    Sub sDownloadHTTP(strURL As String)
        strURL = StrConv(strURL, vbUnicode)
        DoFileDownload (strURL)
    End Sub
    sDownloadHTTP "http://www.mysite.com/myfile.zip"
    Last edited by Aussiebear; 03-02-2025 at 05:31 PM.

  2. #2
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,954
    Location
    Without knowing your DoFileDownload code, there is no way to know. I guess that you could run a routine to check that the file exists and then check the filesize until it no longer changes over some time.

    Here is some code that might give you an idea or two.
    ' http://vbnet.mvps.org/index.html?code/internet/urldownloadtofilenocache.htm
    ' http://www.mrexcel.com/forum/showthread.php?t=116387
    Private Declare Function URLDownloadToFile Lib "urlmon" Alias _
        "URLDownloadToFileA" (ByVal pCaller As Long, _
        ByVal szURL As String, ByVal szFileName As String, _
        ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long
    Private Declare Function DeleteUrlCacheEntry Lib "Wininet.dll" _
        Alias "DeleteUrlCacheEntryA" ( _
        ByVal lpszUrlName As String) As Long
    Private Declare Function GetTempFileName Lib "kernel32" Alias _
        "GetTempFileNameA" (ByVal lpszPath As String, _
        ByVal lpPrefixString As String, ByVal wUnique As Long, _
        ByVal lpTempFileName As String) As Long
    Private Declare Function SetFileAttributes Lib "kernel32" Alias _
        "SetFileAttributesA" (ByVal lpFileName As String, _
        ByVal dwFileAttributes As Long) As Long
    Private Const ERROR_SUCCESS As Long = 0
    Private Const BINDF_GETNEWESTVERSION As Long = &H10
    Private Const INTERNET_FLAG_RELOAD As Long = &H80000000
    Private Const FILE_ATTRIBUTE_TEMPORARY = &H100
    
    Private Function DownloadFile(sSourceURl As String, _
        LocalFile As String) As Boolean
        Dim sLocalFile As String
        ' Download the file. BINDF_GETNEWESTVERSION forces the API to download from the specified source.
        ' Passing 0& as dwReserved causes the locally-cached copy to be downloaded, if available. If the API
        ' returns ERROR_SUCCESS (0), DownloadFile returns True.
        DownloadFile = URLDownloadToFile(0&, _
        sSourceURl, _
        sLocalFile, _
        BINDF_GETNEWESTVERSION, _
        0&) = ERROR_SUCCESS
    End Function
    
    Function LoadPictureUrl(sSourceURl As String) As IPictureDisp
        Dim sLocalFile As String
        On Error GoTo err_h
        ' Create a buffer
        sLocalFile = String(260, 0)
        ' Get a temporary filename
        GetTempFileName "C:\", "KPD", 0, sLocalFile
        ' Remove all the unnecessary chr$(0)'s
        sLocalFile = Left$(sLocalFile, InStr(1, sLocalFile, Chr$(0)) - 1)
        ' Set the file attributes
        SetFileAttributes sLocalFile, FILE_ATTRIBUTE_TEMPORARY
        ' Attempt to delete any cached version of the file.
        DeleteUrlCacheEntry sSourceURl
        If DownloadFile(sSourceURl, sLocalFile) = True Then
            ' hfile = FreeFile
            ' Open sLocalFile For Input As #hfile
            ' Text1.Text = Input$(LOF(hfile), hfile)
            ' Close #hfile
            Set LoadPictureUrl = LoadPicture(sLocalFile)
            Kill sLocalFile
        Else
            ' Create a bogus error
            Err.Raise 999
        End If
        Exit Function
        err_h:
        Set LoadPictureUrl = LoadPicture("")
    End Function
    
    Private Sub Command1_Click()
        Dim Url As String, Image1 As Shape
        Url = "http://www.mrexcel.com/board2/images/avatars/1739339495404fef2bc2773.gif"
        Image1.Picture = LoadPictureUrl(Url)
    End Sub
    Last edited by Aussiebear; 03-02-2025 at 05:34 PM.

  3. #3
    Hi Kenneth,

    sorry about that.

    Public Declare Function DoFileDownload Lib "shdocvw.dll" _
        (ByVal lpszFile As String) As Long
    All I want to do is capture the location where the user puts the file so that it is shows in the textbox, then detect when the download is complete so I could run the rest of my code.
    Last edited by Aussiebear; 03-02-2025 at 05:35 PM.

  4. #4
    I found this and thought I post if here for future use.

    http://www.vbaexpress.com/forum/showthread.php?t=33145

    Tony

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •