Solved

Get images from the URL's in excel

Posted on 2014-07-22
18
886 Views
Last Modified: 2014-07-23
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
0
Comment
Question by:mtthompsons
  • 11
  • 6
18 Comments
 
LVL 13

Expert Comment

by:duncanb7
ID: 40213441
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

0
 
LVL 13

Expert Comment

by:duncanb7
ID: 40213451
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
0
 

Author Comment

by:mtthompsons
ID: 40213485
I want the images into the excel in Column "D" not just download to a drive
Will this code do that?
0
 
LVL 13

Expert Comment

by:duncanb7
ID: 40213517
Already done that, please check the attachment

Just run test() sub in macro1

Duncan
image.xls
0
 
LVL 13

Expert Comment

by:duncanb7
ID: 40213521
Please review the code , you also need to know what is doing on each code

Duncan
0
 

Author Comment

by:mtthompsons
ID: 40213578
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
0
 
LVL 13

Expert Comment

by:duncanb7
ID: 40213585
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

0
 
LVL 13

Expert Comment

by:duncanb7
ID: 40213587
I work because I click the Range("D1') in sheet1  before macro start to run

Duncan
0
 

Author Comment

by:mtthompsons
ID: 40213641
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"
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 13

Expert Comment

by:duncanb7
ID: 40213770
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
0
 
LVL 13

Expert Comment

by:duncanb7
ID: 40213773
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
0
 

Author Comment

by:mtthompsons
ID: 40213795
Thanks
Everything works fine
its just that it copies all images on each other and not d column
Please see the attachment
Sample.JPG
0
 
LVL 13

Expert Comment

by:duncanb7
ID: 40213808
probaby it is Excel version issue , I am using 2003

you are using 2007 or above, right ?
0
 

Author Comment

by:mtthompsons
ID: 40213812
yes i am using 2007
0
 
LVL 13

Expert Comment

by:duncanb7
ID: 40213819
I am downloading Excel 2007, please wait since it is expired

Duncan
0
 
LVL 13

Accepted Solution

by:
duncanb7 earned 400 total points
ID: 40213901
see the attachment and run macro

Duncan
image.xls
0
 
LVL 26

Assisted Solution

by:MacroShadow
MacroShadow earned 100 total points
ID: 40213947
Pictures can never be placed in a cell, the picture layer is a  separate layer. You can place an image over a cell which I believe Duncan is doing (I didn't check any of his samples).

Check the attached file, in column D the image is inserted in a comment and in column E the image is placed over the cell.

Look at the formulas in columns D & E to see how it works. It is possible if you want to create a macro that will insert the images instead of manually inserting formulas.
Sample.xls
0
 

Author Closing Comment

by:mtthompsons
ID: 40213995
Thanks a lot for the help...
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Drop Down List with Unique/Distinct Values (enhancing the Combo-Box with a few steps and a little code) David miller (dlmille) Intro Have you ever created a data validation list from a database field or spreadsheet column (e.g., Zip Codes or Co…
Dealing with unintended Excel Active-X resizing quirks (VBA code simulates "self correction") David Miller (dlmille) Intro Not everyone is a fan of Active-X controls in spreadsheets (as opposed to the UserForm approach, the older Form controls …
Viewers will learn the basics of slicers and timelines for both PivotTables and standard Excel tables in Excel 2013.
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now