3

I have an excel document linked to an SQL database which contains several columns of image URLs.

One of those URLs looks like this: https://imissit.blob.core.windows.net/iris/596480cf967e0c990c37fba3725ada0c/814040e2-0ccb-4b05-bdb3-d9dc9cc798d9/texture.png https://imissit.blob.core.windows.net/iris/596480cf967e0c990c37fba3725ada0c/814040e2-0ccb-4b05-bdb3-d9dc9cc798d9/texture.png

I found different approaches and methods on how to convert those URLs to images ( e.g. Excel VBA Insert Images From Image Name in Column and https://superuser.com/questions/940861/how-can-i-display-a-url-as-an-image-in-an-excel-cell) within the excel document using macros. I tried those approaches but none of them works for my kind of URL. I tried other URLs (random images on the web, http and https and for those images it WORKS).

This is one of the snippets I tried, which works for other images:

   Sub InstallPictures()
    Dim i As Long, v As String
    For i = 2 To 2
        v = Cells(i, "O").Value
        If v = "" Then Exit Sub
        With ActiveSheet.Pictures
            .Insert (v)
        End With
    Next i
End Sub

Anyway when trying it with my URL I get a runtime error 1004: Insert method of picture object cannot be executed(translated). Different approaches result in slightly different runtime errors (although 1004 is consistent).

Here are some image URLs I tried that work:

https://docs.oracle.com/cd/E21454_01/html/821-2584/figures/HTTPS_Collab_Sample.png

http://www.w3schools.com/css/paris.jpg

https://scontent.ftxl1-1.fna.fbcdn.net/v/t1.0-9/13043727_278733959131361_2241170037980408109_n.jpg?oh=bec505696c5f66cde0cc3b574a70547c&oe=58CC35C5

What is different from my URL to the others and why those methods don't work? What is the correct approach?

Community
  • 1
  • 1
4ndro1d
  • 2,926
  • 7
  • 35
  • 65
  • How are you downloading the images? Also, [edit your question](http://stackoverflow.com/posts/40890281/edit) to include your code - it will be next to impossible to answer without it. – Comintern Nov 30 '16 at 14:02
  • Edited. No download at all I guess? – 4ndro1d Nov 30 '16 at 14:03
  • If it works with other URLs, my first guess would be the length of the string. I'd test it with something 255 characters or less. – Comintern Nov 30 '16 at 14:08
  • Well it is only about 125 chars long. I will try to find another image with a similar length tho – 4ndro1d Nov 30 '16 at 14:24
  • Found one, which is working. Appneded those working URLs to the question – 4ndro1d Nov 30 '16 at 14:27
  • The specific problem URL works for me when I test it. – David Zemens Nov 30 '16 at 14:34
  • 1
    Try `.Insert Trim(v)` -- if there are leading/trailing whitespace, that could result as an invalid URL and would raise the 1004. – David Zemens Nov 30 '16 at 14:37
  • 1
    The HTTP response code for the "problem" link is 206 ("partial content", https://httpstatuses.com/206), whereas the working ones are all 200 (success). Thatm ight be the issue. – Tim Williams Nov 30 '16 at 19:48
  • sounds reasonable. any hints on how I could fix this? – 4ndro1d Nov 30 '16 at 20:18
  • @DavidZemens tried it without success. – 4ndro1d Nov 30 '16 at 20:40
  • 1
    Sorry - no ideas about a fix: just pointing out what might be the root of the problem. – Tim Williams Nov 30 '16 at 21:30
  • Here's the thing: the URL is working, with *your* code, when I test it. This tells me the problem isn't in the VBA, but it's on the host end. You can't fix that, you can only handle the error. Do you need help with that aspect of this? If so, let me know :) – David Zemens Dec 01 '16 at 00:31
  • I would really appreciate that. I will try to run it on my laptop as well but probably the problem is on the network site, router firewalls or whatever? really need this data to write my master thesis tho :D I will call for help in case it is not working from the laptop as well – 4ndro1d Dec 01 '16 at 08:23
  • Okay same story here :( Last try I can do is using my mobile phone's data volume and create a hotspot. – 4ndro1d Dec 01 '16 at 12:39
  • Also not working... I am on Windows 10 and used Excel 2016/365. What are you using? – 4ndro1d Dec 01 '16 at 12:43
  • It's probably not *your* network connection, it's the host where the image resides. (Although your network connection could also be a problem) – David Zemens Dec 01 '16 at 15:05
  • I have a similar problem with excel 2016. The issue seems to be that Excel is sending an HTTP 'OPTIONS'- and/or 'HEAD'-request before sending the actual 'GET' request (at least in my tracedown). If the server is not capable of handling those requests the response will be a '405 Method Not Allowed' response, which results in an VBA error. interessting is that it worked for me about 3-4 weeks ago and suddenly stopped working. – Kurt Ludikovsky Jan 01 '19 at 23:10

1 Answers1

2

The problem (as far as I can tell) is not your device, but it's on the server that is hosting the image, and is failing to return the document. I'm not sure where Tim's comment above (pertaining to the 206 response code) comes from, but if that's the case, or if the URL is returning some error code, then your VBA would also fail and there is likely nothing you can do to resolve that if the problem is at the host.

I manually enter the URL today and download the file, no problem.

I check the response code and it's returning correctly a 200 (success).

enter image description here

The best you can do at this point is to simply trap the error, and flag it for later review.

In my test, I used some deliberatly bad URL just to ensure error handling is working as expected. These are the only ones that failed for me.

enter image description here

Here's the code I used, modified only slightly from yours and includes an error-handler to add a COMMENT to the cells which URLs return the error. This way you can later review manually and add those images if needed.

Sub InstallPictures()
    Dim i As Long
    Dim v As String
    Dim cl As Range
    Dim pic As Shape
    Dim errors As New Collection

    i = 2
    Set cl = Cells(i, 15)
    Do While Trim(cl.Value) <> vbNullString
        v = Trim(cl.Value)
        cl.ClearComments

        With ActiveSheet.Pictures
            On Error GoTo ErrHandler
            Set p = .Insert(Trim(v))
            On Error GoTo 0
            ' I added this code to resize & arrange the pictures
            ' you can remove it if you don't need it
            p.TopLeftCell = cl.Offset(0, -1)
            p.Top = cl.Offset(0, -1).Top
            p.Left = cl.Offset(0, -1).Left
            p.Height = Cells(i, 15).Height
            p.Width = Cells(1, 15).Width
            '''''''''''''''''''''''''''''
        End With

NextCell:
        i = i + 1
        Set cl = Cells(i, 15)
    Loop

    If errors.Count > 0 Then
        MsgBox "There were errors, please review the comments as some files may need to be manually downloaded"
    End If

    Exit Sub


ErrHandler:
    Call ErrorNote(v, cl, errors)
    Resume NextCell
End Sub

Private Sub ErrorNote(url$, cl As Range, ByRef errs As Collection)
' Adds an item to the errs collection and flags the offending
' cell with a Comment indicating the error occurred. 
    On Error Resume Next
    errs.Add (url)
    With cl
        .ClearComments
        .AddComment ("Error with URL: " & vbCrLf & url)
    End With
End Sub
David Zemens
  • 53,033
  • 11
  • 81
  • 130
  • Error with URL comment - nothing else :( I don't really get it ^^ This is how it looke for me: https://s17.postimg.org/zfd6bw17z/Capture.png – 4ndro1d Dec 02 '16 at 12:36
  • It is working. On my wifes's laptop.. I seriously don't know why because it's basically the same model as mine running the same software. Only difference was the logged in microsoft account. If I find some time I will try to confirm that.. – 4ndro1d Dec 02 '16 at 13:16