File download from Internet in VBA (1 Viewer)

ddaddy

Registered User.
Local time
Today, 04:06
Joined
Feb 26, 2008
Messages
20
I want to make an autodownload and update script for my DB.
I have found 2 different ways to download a file from the internet through vba, but each has its problem.

Code:
Option Compare Database
Option Explicit

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

Sub DownloadFileFromWeb()
On Error GoTo err_1
    Const strUrl As String = "http://www.puremis.net/excel/index.html"
    Dim strSavePath As String
    Dim returnValue As Long
    strSavePath = CurrentProject.Path & "\tempupdate2.html"
    returnValue = URLDownloadToFile(0, strUrl, strSavePath, 0, 0)

Err_Exit:
    Exit Sub
    
err_1:
    MsgBox err.Description
    Resume Err_Exit
End Sub

The problem with the above, is that it freezes up the DB while it downloads. This means I cant use a Progress Bar to let the user know that something is actually happening.
Also, if the cache isn't cleared after downloading once, it will use the cached file if run a second time.

Code:
Option Compare Database
Option Explicit

Public Declare Function DoFileDownload Lib "shdocvw.dll" _
        (ByVal lpszFile As String) _
        As Long

Sub sDownloadHTTP(strURL As String)
    strURL = StrConv(strURL, vbUnicode)
    DoFileDownload (strURL)
End Sub

This code will actually show the download progress automatically, but the problem is that it asks for a location to save the file.

Does anyone know of a way to use a progress bar with the first code, and a way to clear the cached file before downloading?
Or how to automatically set a download location with the 2nd code?
Or an alternativce 3rd code?

Many Thanks
 

ulfemsoy

New member
Local time
Yesterday, 20:06
Joined
Nov 1, 2011
Messages
1
You can find some code on downloading files from VBA here:
lazerwire.com/2011/11/excel-vba-download-files-from-internet.html
 

Users who are viewing this thread

Top Bottom