[COLOR=darkblue]Option[/COLOR] [COLOR=darkblue]Explicit[/COLOR]
[COLOR=darkblue]Sub[/COLOR] DownloadImages()
[COLOR=darkblue]Dim[/COLOR] sURL [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
[COLOR=darkblue]Dim[/COLOR] sDestPath [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
[COLOR=darkblue]Dim[/COLOR] sDestFile [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
[COLOR=darkblue]Dim[/COLOR] i [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
[COLOR=darkblue]Dim[/COLOR] LastRow [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
[COLOR=green]'Turn off screen updating[/COLOR]
Application.ScreenUpdating = [COLOR=darkblue]False[/COLOR]
[COLOR=green]'Set the path to the destination folder (change accordingly)[/COLOR]
sDestPath = "C:\Users\Domenic\Desktop\"
[COLOR=darkblue]If[/COLOR] Right(sDestPath, 1) <> "\" [COLOR=darkblue]Then[/COLOR]
sDestPath = sDestPath & "\"
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
[COLOR=green]'Find the last used row in Column A[/COLOR]
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
[COLOR=green]'Loop through each cell in Column A starting at Row 2[/COLOR]
[COLOR=darkblue]For[/COLOR] i = 2 [COLOR=darkblue]To[/COLOR] LastRow
[COLOR=green]'Check whether cell constains a hyperlink[/COLOR]
[COLOR=darkblue]If[/COLOR] Cells(i, "A").Hyperlinks.Count > 0 [COLOR=darkblue]Then[/COLOR]
[COLOR=green]'Get the URL from the current cell[/COLOR]
sURL = Cells(i, "A").Hyperlinks.Item(1).Address
[COLOR=green]'Call function to download the URL file to the destination folder[/COLOR]
[COLOR=darkblue]If[/COLOR] [COLOR=darkblue]Not[/COLOR] SaveWebFile(sURL, sDestPath & Mid(sURL, InStrRev(sURL, "/") + 1)) [COLOR=darkblue]Then[/COLOR]
[COLOR=green]'If URL file isn't found, mark the corresponding cell in Column B as N/A (optional)[/COLOR]
Cells(i, "B").Value = "N/A"
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
[COLOR=darkblue]Next[/COLOR] i
[COLOR=green]'Turn on screen updating[/COLOR]
Application.ScreenUpdating = [COLOR=darkblue]True[/COLOR]
MsgBox "Completed!", vbInformation
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]
[COLOR=darkblue]Function[/COLOR] SaveWebFile(sURL [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR], sDestinationFile [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]) [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Boolean[/COLOR]
[COLOR=darkblue]Dim[/COLOR] oXMLReq [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Object[/COLOR]
[COLOR=darkblue]Dim[/COLOR] oResp() [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Byte[/COLOR]
[COLOR=darkblue]Dim[/COLOR] FileNum [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
[COLOR=green]'Create an instance of the XML object[/COLOR]
[COLOR=darkblue]Set[/COLOR] oXMLReq = CreateObject("MSXML2.XMLHTTP")
[COLOR=darkblue]With[/COLOR] oXMLReq
[COLOR=green]'Open a socket to get the URL[/COLOR]
.Open "GET", sURL, [COLOR=darkblue]False[/COLOR]
[COLOR=green]'Send the request[/COLOR]
.send
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
[COLOR=green]'Check whether the request has been successful (200 = OK)[/COLOR]
[COLOR=darkblue]If[/COLOR] oXMLReq.Status <> 200 [COLOR=darkblue]Then[/COLOR]
SaveWebFile = [COLOR=darkblue]False[/COLOR]
[COLOR=darkblue]Exit[/COLOR] [COLOR=darkblue]Function[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
[COLOR=green]'Get the response from the request (returns a byte array)[/COLOR]
oResp = oXMLReq.responseBody
[COLOR=green]'Download the file to the destination[/COLOR]
FileNum = FreeFile
[COLOR=darkblue]Open[/COLOR] sDestinationFile [COLOR=darkblue]For[/COLOR] [COLOR=darkblue]Binary[/COLOR] [COLOR=darkblue]As[/COLOR] [URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FileNum"]#FileNum[/URL]
Put [URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FileNum"]#FileNum[/URL] , , oResp
[COLOR=darkblue]Close[/COLOR] [URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FileNum"]#FileNum[/URL]
[COLOR=green]'Clear from memory[/COLOR]
[COLOR=darkblue]Set[/COLOR] oXMLReq = [COLOR=darkblue]Nothing[/COLOR]
SaveWebFile = [COLOR=darkblue]True[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Function[/COLOR]