Excel convert URL to images (1004)

前端 未结 1 1898
小蘑菇
小蘑菇 2021-01-21 07:40

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

相关标签:
1条回答
  • 2021-01-21 07:49

    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).

    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.

    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
    
    0 讨论(0)
提交回复
热议问题