Sub DownloadImages()
Dim myURL As String
Dim cell, dlPath$, NomImg$, imgSrc$, splitter
Dim WinHttpReq As Object, oStream As Object, RegEx As Object, Matches As Object
Set WinHttpReq = CreateObject("WinHttp.WinHttpRequest.5.1")
Set oStream = CreateObject("ADODB.Stream")
Set RegEx = CreateObject("VBScript.RegExp")
RegEx.Pattern = "[^/\\]+$" ' Motif pour extraire nom d'image dans URL
dlPath = "D:\tmp\dlImages\"
For Each cell In Sheets("Feuil1").Range("Urls")
imgSrc = cell
Set Matches = RegEx.Execute(imgSrc)
If Matches.Count = 1 Then
splitter = Split(Matches(0), "?")
NomImg = splitter(0)
Debug.Print NomImg
Else
Cells.Offset(0, 3) = "Nom d'image incorrect"
GoTo NextIteration
End If
On Error GoTo NextIteration
WinHttpReq.Open "GET", imgSrc, False
WinHttpReq.send
' Debug.Print imgSrc, WinHttpReq.Status
If WinHttpReq.Status = 200 Then
oStream.Open
oStream.Type = 1
oStream.Write WinHttpReq.responseBody
oStream.SaveToFile dlPath & NomImg, 2 ' 1 = no overwrite, 2 = overwrite
oStream.Close
cell.Offset(0, 1) = "OK"
cell.Offset(0, 3) = NomImg
Else
cell.Offset(0, 1) = "NOK"
cell.Offset(0, 2) = WinHttpReq.Status
End If
NextIteration:
Next cell
Set WinHttpReq = Nothing: Set oStream = Nothing: Set Matches = Nothing: Set RegEx = Nothing
MsgBox "Vérification terminée !"
End Sub