Download pictures from URLs en masse

blogospherexcel

New Member
Joined
Feb 14, 2022
Messages
2
Office Version
  1. 2007

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
First make sure that your url includes 'http://' or 'https://' at the beginning so that your data looks like this . . .


Note that the alternative would be to amend the code to prepend the appropriate prefix. Then try the following code, which assumes that the worksheet containing the data is the active sheet.

VBA Code:
Option Explicit

Sub test()

    Dim mainFolder As String
    mainFolder = "c:\users\domenic\desktop\"
    If Right(mainFolder, 1) <> "\" Then
        mainFolder = mainFolder & "\"
    End If

    Dim lastRow As Long
    lastRow = Cells(Rows.Count, "A").End(xlUp).Row
 
    Dim i As Long
    Dim destinationFolder As String
    Dim url As String
    Dim errorMessage As String
    For i = 2 To lastRow
        destinationFolder = Cells(i, "A").Value
        url = Cells(i, "B").Value
        If Len(destinationFolder) > 0 And Len(url) > 0 Then
            DownloadFile mainFolder & destinationFolder, url
        Else
            Cells(i, "C").Value = "Item and/or File URL missing"
        End If
    Next i
 
    MsgBox "Completed!", vbExclamation
 
End Sub

Public Function DownloadFile(ByVal pathToFolder As String, ByVal url As String, Optional ByRef errorMessage As String = "") As Boolean

    On Error GoTo errorHandler
 
    Dim xmlhttp As Object
    Set xmlhttp = CreateObject("MSXML2.XMLHTTP")
 
    With xmlhttp
        .Open "GET", url, False
        .send
        Do While .readystate <> 4
            DoEvents
        Loop
        If .Status <> 200 Then
            errorMessage = "Error " & .Status & ": " & .statustext
            Set xmlhttp = Nothing
            DownloadFile = False
            Exit Function
        End If
        Dim response() As Byte
        response() = .responsebody
    End With
 
    If Right(pathToFolder, 1) <> "\" Then
        pathToFolder = pathToFolder & "\"
    End If
 
    Dim filename As String
    filename = Mid(url, InStrRev(url, "/") + 1)
 
    Dim fileNumber As Long
    fileNumber = FreeFile()
 
    Open pathToFolder & filename For Binary As #fileNumber
        Put #fileNumber, , response
    Close #fileNumber
 
    DownloadFile = True
 
exitHandler:
    Set xmlhttp = Nothing
    Exit Function
 
errorHandler:
    errorMessage = "Error " & Err.Number & ":" & vbCrLf & vbCrLf & Err.Description
    DownloadFile = False
    Resume exitHandler
 
End Function

However, in some cases, there may be errors that occur during the course of the running the code. To flag any errors that occur and write them in Column C, replace . . .

VBA Code:
DownloadFile mainFolder & destinationFolder, url

with

VBA Code:
            If Not DownloadFile(mainFolder & destinationFolder, url, errorMessage) Then
                Cells(i, "C").Value = errorMessage
            End If

So let's say that you had the following data . . .


The result would be as follows . . .

ItemFile URL
Treetree1.jpgError -2147012890: Method 'open' of object 'IServerXMLHTTPRequest2' failed
Treewww.fubar.com/tree2.jpgError -2147012890: Method 'open' of object 'IServerXMLHTTPRequest2' failed
Pighttp://www.fubar.com/pig1.jpg
Pighttps://www.fubar.com/pig2.jpgError -2147024891: Access is denied.
FlowerItem and/or File URL missing
http://www.fubar.com/flower1.jpgItem and/or File URL missing
Item and/or File URL missing
Flowershttp://www.fubar.com/flower2.jpgError 76: Path not found

Hope this helps!
 
Upvote 0
Solution
Thanks, Domenic, that worked like a charm!

One more question. In my URLs, there are many non-English characters such as "À", "う", etc. How do I let VBA handle these cases?
 
Upvote 0
You may need to encode your URL string.

With Excel 2013 or later version, you can use the EncodeURL method of the WorksheetFunction object.

However, since you're using an earlier version, have a look at the following link . . .

 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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