Friday, February 17, 2012

Excel macro to scrape the image from image url

If you would like to download the images from more than 1000 URLs, here is the excel macro to download all the images at one go.

The macro will automatically download all the jpg files from the given urls.

Macro 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 Function DownloadURLtoFile(sSourceURL As String, _
sLocalFileName As String) As Boolean


DownloadURLtoFile = URLDownloadToFile(0&, _
sSourceURL, sLocalFileName, &H10, 0&) = 0&

End Function

Sub DownLoadPics()
Dim cell As Range, rngListOfURL As Range

PTH = Cells(2, 4)

Set rngListOfURL = Sheet1.Range("A2:A1354")

For Each cell In rngListOfURL
If DownloadURLtoFile(cell.Value, PTH & cell.Offset(, 1).Value & ".jpg") Then
cell.Offset(, 2).Value = "Successfully downloaded"
Else
cell.Offset(, 2).Value = "Error - no download"
End If
Next cell

End Sub

4 comments:

  1. THANKS FOR THE POST
    WERE WILL THE IMAGES BE DOWNLOADED TO? AND IS IT POSSIBLE TO CHANGE THE DOWNLOAD DESTINATION FOLDER

    ReplyDelete
    Replies
    1. Yes. The destination of the folder is mentioned in the code in the following line:

      PTH = Cells(2, 4)

      Have to mention the path here.
      If you need any customized macro for you, you can contact me here:

      http://fiverr.com/dsenthilbe/create-any-type-of-macro-in-excel

      Delete
    2. Hi Boss,
      Images not downloading in the folder. Could you please help me?

      Delete
    3. Hi Suresh,

      The destination folder path should be mentioned in D2 and path should end with '\'

      Delete