Showing posts with label download images using macro. Show all posts
Showing posts with label download images using macro. Show all posts

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