Link to home
Start Free TrialLog in
Avatar of mtthompsons
mtthompsons

asked on

Get images from the URL's in excel

Hi All,

Attached file is a sample. I have the URL's in Column "C" which are of images. I want help with a macro that can check the URL and fetch the images and paste into column "D"

Can anyone help me please as i have 100's to be gathered

Thanks
Sample.xls
Avatar of duncanb7
duncanb7

You need download function URLDownloadToFile on vba to do,
run the macro1 test() as follows, and save image files at C;\temp\

Hope understand your question.if not, please point it out

Duncan

Private Declare Function URLDownloadToFile Lib "urlmon" _
Alias "URLDownloadToFileA" (ByVal pCaller As Long, ByVal szURL As String, _
ByVal szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long

Private Const ERROR_SUCCESS As Long = 0



Sub test()

Dim tmp, i As Integer
Dim slocalfile As String
'Delete all image
Dim Pic As Object
For Each Pic In ActiveSheet.Pictures
Pic.Delete
Next Pic

'import image
tmp = Range(Range("C1"), Range("C65535").End(xlUp)).Count
Debug.Print "Starting loading and importing image"
For i = 1 To tmp
Debug.Print Cells(i, 3).Value
slocalfile = "C:\temp\c" & i & ".png"
Call URLDownloadToFile(0&, Cells(i, 3).Value, slocalfile, 0&, 0&)
 Cells(i, 4).Select
    Selection.ClearContents
    ActiveSheet.Pictures.Insert(slocalfile).Select
    
Next i

End Sub

Open in new window

be reminded you need to  have c:\temp directory to save the image locally, otherwise change it to other directory you want on VBA

Duncan
Avatar of mtthompsons

ASKER

I want the images into the excel in Column "D" not just download to a drive
Will this code do that?
Already done that, please check the attachment

Just run test() sub in macro1

Duncan
image.xls
Please review the code , you also need to know what is doing on each code

Duncan
I get the item with the specified name is not found

I do have a folder called temp in C Drive

When debug goes to this line
    ActiveSheet.Shapes("Picture 1").Select
it might be you did not select sheet and cell so that there is not Activate

put those two code into test()
ActiveWorkbook.Sheets("sheet1").Activate
ActiveWorkbook.Sheets("sheet1").Range("D1").Select

Duncan

Sub test()
Dim tmp, i As Integer
Dim slocalfile As String
'Delete all image
Dim Pic As Object
For Each Pic In ActiveSheet.Pictures
Pic.Delete
Next Pic
ActiveWorkbook.Sheets("sheet1").Activate
ActiveWorkbook.Sheets("sheet1").Range("D1").Select
'import image
tmp = Range(Range("C1"), Range("C65535").End(xlUp)).Count
Debug.Print "Starting loading and importing image"
For i = 1 To tmp
Debug.Print Cells(i, 3).Value
slocalfile = "C:\temp\" & i & ".png"
Call URLDownloadToFile(0&, Cells(i, 3).Value, slocalfile, 0&, 0&)
 Cells(i, 4).Select
    Selection.ClearContents
    ActiveSheet.Pictures.Insert(slocalfile).Select
    
Next i

End Sub

Open in new window

I work because I click the Range("D1') in sheet1  before macro start to run

Duncan
I ran the macro called Test and selected sheet 1 column "D" but all images are being collected but all one over the other in column "A"
just run the attachment , by click atl-F11 go to VBA on Excel and run test() macro and no need to do any select( it is already done for you).

review all my previous post and think about each code function


Duncan
image.xls
Please read it, Tutorial for Excel VBA for activate and select  at
http://msdn.microsoft.com/en-us/library/office/aa221576(v=office.11).aspx
Thanks
Everything works fine
its just that it copies all images on each other and not d column
Please see the attachment
Sample.JPG
probaby it is Excel version issue , I am using 2003

you are using 2007 or above, right ?
yes i am using 2007
I am downloading Excel 2007, please wait since it is expired

Duncan
ASKER CERTIFIED SOLUTION
Avatar of duncanb7
duncanb7

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Joe Howard
Joe Howard
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks a lot for the help...