17

I'm creating a csv file with one of the columns containing an url of an image (e.g., www.myDomain.com/myImage.jpg).
How I can get Excel to render this image?

GSerg
  • 76,472
  • 17
  • 159
  • 346
RayLoveless
  • 19,880
  • 21
  • 76
  • 94

3 Answers3

27
Dim url_column As Range
Dim image_column As Range

Set url_column = Worksheets(1).UsedRange.Columns("A")
Set image_column = Worksheets(1).UsedRange.Columns("B")

Dim i As Long
For i = 1 To url_column.Cells.Count

  With image_column.Worksheet.Pictures.Insert(url_column.Cells(i).Value)
    .Left = image_column.Cells(i).Left
    .Top = image_column.Cells(i).Top
    image_column.Cells(i).EntireRow.RowHeight = .Height
  End With

Next

As Excel behaviour has apparently changed over years, you might want to specify more parameters to the Insert call explicitly:

For people landing here. Different versions of Excel handle this request differently, Excel 2007 will insert the picture as an object, ie embed it in the workbook. Excel 2010 will insert it as a link, which is bad times if you plan on sending it to anyone. You need to change the insert to specify that it is embedded: Insert(Filename:= <path>, LinkToFile:= False, SaveWithDocument:= True)

GSerg
  • 76,472
  • 17
  • 159
  • 346
  • Thanks I believe this will work. Sorry for the late response. – RayLoveless Jan 10 '12 at 21:03
  • 3
    So does this "render the image" (as asked in the question) or does it "insert the image into a cell"? I am looking for a solution that renders images in Excel, based on URLs in other cells, but don't want the images to be embedded in the spreadsheet and add to its size when saving. Am I right in thinking this is not the solution? – Jason Sep 04 '13 at 14:18
  • @Jason Yes, it is not the solution. You can delete the inserted images in e.g. `Workbook_BeforeSave` though. – GSerg Sep 04 '13 at 14:21
  • 1
    I really don't get how this solution is supposed to work / be used ! – Stéphane Nov 05 '13 at 16:47
  • @Stéphane Create a `sub` in a module or in a worksheet. Name it as you wish. Paste the code between `Sub` and `End Sub`. Adjust the `Worksheets(1)` part to be whatever suits you, if needed. Run. – GSerg Nov 05 '13 at 17:10
  • @avalanche1 Why not? – GSerg Feb 01 '19 at 18:59
  • @GSerg, works, my bad. smth wrong with the host that refuses connections from vba for some reason – avalanche1 Feb 04 '19 at 14:43
  • 1
    @avalanche1 Probably because Excel sends some funny user agent like IE6.0. – GSerg Feb 04 '19 at 16:24
7

On Google Sheets you can add the IMAGE(url) method to your CSV file and it will be rendered (Tested on Google Sheets).

Here is an example:

=IMAGE("http://efdreams.com/data_images/dreams/lion/lion-03.jpg")
Sahar Menashe
  • 1,945
  • 2
  • 18
  • 17
0

I had same issue. My solution was:

  1. import csv to Excel,
  2. from excel copy table to Google Sheets
  3. in Google Sheets use =IMAGE("http://example.com/01.jpg") on column you want to have images. Google Sheets load the images,
  4. just copy all back to Excel.

In excel images will be in original sizes but always started on apropriate row, so just select all and change width or height of all images. If you change only height by height of the row, you will have everything ordered and positioned right.