Download a file from a URL

 

Sample VBA code for downloading a file or web page from a URL:

 

 

'--- place this code in a standard module ---'
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

 

Public Sub bimDownloadURLtoFile(pURL As String, _
     pFullFilePath As String)

 

   Call URLDownloadToFile(0, pURL, pFullFilePath, 0, 0)

 

End Sub

 

''' end code ''' 

 

 

It is possible to pass the username and password on the URL like this:

http://username:password@hostname/pathtofile
or
ftp://username:password@hostname/pathtofile

 

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


Public Sub bimDownloadURLtoFile(pURL As String, pFullFilePath As String)

' save the whatismyip.com automation page to file
    Call URLDownloadToFile(0, pURL, pFullFilePath, 0, 0)


End Sub



Public Sub TestIt()


Call bimDownloadURLtoFile("http://images.mximg.com/mdnh/sitebox/splash/Computers_and_Internet/Computers_and_Internet37701779.jpg", "C:\Users\boyd\Documents\test.jpg")

End Sub