|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
Option Explicit
Sub TEST1()
Dim ar, i&, n&, strPath$, strFileName$
Application.DisplayAlerts = False
ar = [A1].CurrentRegion.Value
strPath = ThisWorkbook.Path & "\"
For i = 2 To UBound(ar)
strFileName = strPath & ar(i, 2) & ".jpg"
If DownloadImage(CStr(ar(i, 1)), strFileName) = True Then
n = n + 1
End If
Next i
Application.DisplayAlerts = True
MsgBox "共下载" & n & "张图片!"
Beep
End Sub
Function DownloadImage(strUrl As String, strFileName As String) As Boolean
Dim httpReq As Object
On Error GoTo ErrorHandler
Set httpReq = CreateObject("MSXML2.XMLHTTP")
With httpReq
.Open "GET", strUrl, False
.Send
End With
If httpReq.Status = 200 Then
With CreateObject("ADODB.Stream")
.Open
.Type = 1
.Write httpReq.responseBody
.SaveToFile strFileName, 2
.Close
End With
DownloadImage = True
Else
DownloadImage = False
End If
Exit Function
ErrorHandler:
DownloadImage = False
End Function
|
|