Save Images To Disk Using the URLs In A Spreadsheet

miketica

New Member
Joined
Feb 27, 2018
Messages
1
Hi,

I have a spreadsheet with a bunch of image URLs (A2:A1550). The URL format is
domain.com/blah/123456.jpg or
domain.com/blah/123456.png The numerical part varies (image names are different). I want to save all the images to a local folder, let's say C:\Temp using the name after the last / in the URL as a file name (e.g. 123456.jpg)

Separately, it would be nice to be able to resize those images in Excel to width 567 and height 567 before saving them. The reason why I am asking for this separately is because I am not sure if all the images have the same dimensions (if width=height for all of them) and I do not want them to be cropped.

Thanks
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Try doing it like this:

Code:
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 Download_Procedure()
  Dim strTargetFolder As String
  Dim strTargetPath As String
  Dim strSourceUrl As String
  Dim lngNumFiles As Long
  Dim lngErrors As Long
  Dim rngUrls As Range
  Dim rngUrl As Range
  
  On Error GoTo ErrHandler
  Set rngUrls = ThisWorkbook.Sheets("Sheet1").Range("A2:A1550") '<-- Set range containing the URLs
  lngNumFiles = rngUrls.Cells.Count
  
  strTargetFolder = "C:\Temp\" '<-- Set target folder
  If Right(strTargetFolder, 1) <> "\" Then strTargetFolder = strTargetFolder & "\"
  If Dir(strTargetFolder, vbDirectory) = "" Then
    MsgBox "Folder does not exist:" & vbCrLf & strTargetFolder, vbExclamation
    Exit Sub
  End If
  
  For Each rngUrl In rngUrls
    On Error GoTo LoopHandler
    strSourceUrl = rngUrl.Value
    strTargetPath = strTargetFolder & FilenameFromUrl(strSourceUrl)
    If Not DownloadFile(strSourceUrl, strTargetPath) Then
      lngErrors = lngErrors + 1
    End If
    GoTo ContinueFor
LoopHandler:
    If Err.Number <> 0 Then
      lngErrors = lngErrors + 1
      Err.Clear
    End If
ContinueFor:
  Next rngUrl
  
  On Error GoTo ErrHandler
  MsgBox Format(lngNumFiles - lngErrors, "#,0") & " files out of " _
       & Format(lngNumFiles, "#,0") & " were downloaded successfully.", vbInformation
  Exit Sub
  
ErrHandler:
  MsgBox Err.Description, vbExclamation
End Sub

Private Function DownloadFile(strSourceUrl As String, strTargetPath As String) As Boolean
  On Error GoTo ErrHandler
  DownloadFile = (URLDownloadToFile(0, strSourceUrl, strTargetPath, 0, 0) = 0)
  Exit Function

ErrHandler:
  DownloadFile = False
End Function

Private Function FilenameFromUrl(strUrl As String) As String
  Dim lngLength As Long
  Dim lngStart As Long
  Dim lngChars As Long
  
  On Error GoTo ErrHandler
  lngLength = Len(strUrl)
  lngStart = InStrRev(strUrl, "/")
  lngChars = lngLength - lngStart
  FilenameFromUrl = Mid(strUrl, lngStart + 1, lngChars)
  Exit Function
  
ErrHandler:
  FilenameFromUrl = ""
End Function
 
Upvote 0

Forum statistics

Threads
1,223,895
Messages
6,175,257
Members
452,625
Latest member
saadat28

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top