Using VBA to download a file

  • Thread starter Thread starter Zoney
  • Start date Start date
I haven't actually tried downloading an executable with this code, but have
successfully used it with image files, so I don't see why it shouldn't work.

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

Function DownloadFile( _
URL As String, _
LocalFilename As String _
) As Boolean
On Error GoTo Err_DownloadFile

Dim lngRetVal As Long

lngRetVal = _
URLDownloadToFile(0, URL, LocalFilename, 0, 0)
DownloadFile = (lngRetVal = 0)

End Function


You'd use it as

Call DownloadFile "http://www.server.com/filename.exe", _
"C:\SomeFolder\filename.exe"
 
Thanks a lot for that - works a treat.

Is there a way to force the Access window to regain focus whilst it is
running code or downloading?

For instance, if I start say a 10mb download, and alt-tab out whilst it is
downloading, when I alt-tab back to access the Access window doesn't display
or respond until the download has completed - it says not respnding until the
download completes.
 
The fifth argument in the API declaration (lpfnCB) is intended to be the
address of the caller's IBindStatusCallback interface, but I've never seen a
good example of how to use it from VBA.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top