直接上代码,本例参考文件在后附的链接中下载。
Sub Get_PIC_Via_WebLinks()
Set req = CreateObject("Microsoft.XMLHTTP")
Set stm = CreateObject("ADODB.Stream")
pa = Environ("temp") & "\PIC"
If Not CreateObject("Scripting.FileSystemObject").folderExists(pa) Then CreateObject("Scripting.FileSystemObject").Createfolder (pa)
For Each h In Sheet1.Hyperlinks
file = pa & Application.PathSeparator & h.TextToDisplay
If CreateObject("Scripting.FileSystemObject").fileExists(file) Then file = file & Rnd
req.Open "GET", h.Address, False
req.send
If req.Status = 200 Then
stm.Open
stm.Type = 1
stm.write req.responsebody
stm.savetofile file, 2
stm.Close
End If
DoEvents
Next
MsgBox "图片已下载到: " & Environ("temp") & "\PIC"
Set req = Nothing: Set stm = Nothing
End Sub
注:上述在代码中用到的,Appplicaiton对象,Excel表对象,单元格对象,系统环境变量Environ, FSO对象的常用属性和方法,已经在下面的贴子《VBA对象的常用操作方法》中纪录过。
(ADO对象,已纪录过Connection和RecordsSet对象使用方法,本例Stream是ADO的第3个对象用法),XML对象目前还未提及,待后续中更新。。。
VBA对象的常用操作方法:https://tieba.baidu.com/p/8848588841
Sub Get_PIC_Via_WebLinks()
Set req = CreateObject("Microsoft.XMLHTTP")
Set stm = CreateObject("ADODB.Stream")
pa = Environ("temp") & "\PIC"
If Not CreateObject("Scripting.FileSystemObject").folderExists(pa) Then CreateObject("Scripting.FileSystemObject").Createfolder (pa)
For Each h In Sheet1.Hyperlinks
file = pa & Application.PathSeparator & h.TextToDisplay
If CreateObject("Scripting.FileSystemObject").fileExists(file) Then file = file & Rnd
req.Open "GET", h.Address, False
req.send
If req.Status = 200 Then
stm.Open
stm.Type = 1
stm.write req.responsebody
stm.savetofile file, 2
stm.Close
End If
DoEvents
Next
MsgBox "图片已下载到: " & Environ("temp") & "\PIC"
Set req = Nothing: Set stm = Nothing
End Sub
注:上述在代码中用到的,Appplicaiton对象,Excel表对象,单元格对象,系统环境变量Environ, FSO对象的常用属性和方法,已经在下面的贴子《VBA对象的常用操作方法》中纪录过。
(ADO对象,已纪录过Connection和RecordsSet对象使用方法,本例Stream是ADO的第3个对象用法),XML对象目前还未提及,待后续中更新。。。
VBA对象的常用操作方法:https://tieba.baidu.com/p/8848588841