ferocious12
Member
Hi All,
I am using the following code to copy images from a website (amazon in this case) based on URL of the images in column A. I am not sure how can I change the code so the picture related to each cell in column A appear in corresponding cell in column B. Currently, all the pics are pasted in one cell.
I have attached a sample
thanks for the help !
I am using the following code to copy images from a website (amazon in this case) based on URL of the images in column A. I am not sure how can I change the code so the picture related to each cell in column A appear in corresponding cell in column B. Currently, all the pics are pasted in one cell.
I have attached a sample
thanks for the help !
Code:
Function URLExists(sURL As String) As Boolean
Dim XMLReq As Object
On Error GoTo ErrHandler
Set XMLReq = CreateObject("MSXML2.XMLHTTP")
With XMLReq
.Open "GET", sURL, False
.Send
URLExists = (.Status = 200)
End With
ErrHandler:
Set XMLReq = Nothing
End Function
'http://www.vbaexpress.com/kb/getarticle.php?kb_id=799
Function SaveWebFile(ByVal vWebFile As String, ByVal vLocalFile As String) As Boolean
Dim oXMLHTTP As Object, i As Long, vFF As Long, oResp() As Byte
'You can also set a ref. to Microsoft XML, and Dim oXMLHTTP as MSXML2.XMLHTTP
Set oXMLHTTP = CreateObject("MSXML2.XMLHTTP")
oXMLHTTP.Open "GET", vWebFile, False 'Open socket to get the website
oXMLHTTP.Send 'send request
'Wait for request to finish
Do While oXMLHTTP.readyState <> 4
DoEvents
Loop
oResp = oXMLHTTP.ResponseBody 'Returns the results as a byte array
'Create local file and save results to it
vFF = FreeFile
If Dir(vLocalFile) <> "" Then Kill vLocalFile
Open vLocalFile For Binary As #vFF
Put #vFF, , oResp
Close #vFF
'Clear memory
Set oXMLHTTP = Nothing
End Function
Sub URLPictureInsert()
Dim ws As Worksheet
Dim rRange As Range
Dim rCell As Range
Dim sURL As String
Dim sTempFile As String
Dim LastRow As Long
Application.ScreenUpdating = False
Set ws = Worksheets("URL Report")
With ws
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
Set rRange = .Range("A1:A" & LastRow)
End With
For Each rCell In rRange
If Len(rCell) > 0 Then
sURL = rCell.Value
If URLExists(sURL) Then
sTempFile = Environ("temp") & "\" & Mid(sURL, InStrRev(sURL, "/") + 1)
SaveWebFile sURL, sTempFile
ws.Shapes.AddPicture _
Filename:=sTempFile, _
LinkToFile:=msoFalse, _
SaveWithDocument:=msoTrue, _
Left:=rCell.Offset(, 1).Left, _
Top:=rCell.Top, _
Width:=100, _
Height:=100
Kill sTempFile
Else
rCell.Offset(, 1).Value = "File not found" 'optional
End If
End If
Next rCell
Application.ScreenUpdating = True
End Sub
Attachments
Last edited: