By using expertatexcel.com you agree to our cookie policy, We and our partners operate globally and use cookies, for multiple purposes

Become an Σxpert at Σxcel.com

 


DownloadFileFromWeb(strURL, savetoPath) - udf using api

This code can be retrieved from the Web.

It is passed a URL which is a file and stores the file to the passed saveToPath

Google: VBA DownloadFileFromWeb


Function DownloadFileFromWeb(sURL as String, sPath as String)
  On Error Resume Next
  DownloadFileFromWeb = URLDownloadToFile(0, sURL, sPath, 0, 0)
End Function


In the (Declarations) section of a module add

Private Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" (ByVal pCaller As Long, _
   ByVal scURL As String, ByVal szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long



and the whole calling sequence


sFile = Range("DQ_File") ' note range name in spreadsheet with http://Sharepoint3.company.com/..../xyz.


s = Now()
sZipFile = "H:\test & format(s, "yyyy-mm-dd") & ".zip"
On Error Resume Next
Kill sZipFile

x = DownloadFileFromWeb(sFile, (sZipfile))
if x <> 0 then
   MsgBox  x & " " & Error(x) & " while downloading " & sZipFile
   Stop
End if