VBA execution stalls while inserting a pic from a URL

PeterBunde

New Member
Joined
Dec 7, 2016
Messages
45
Fellow sufferers

My below code stalls somewhere between
Code:
Debug.Print "Inserting pic from URL " & strShpUrl
and
Code:
Debug.Print "Inserted pic from URL " & strShpUrl
. I am new to errorhandling, I tried to read everything there is, so whats wrong with my code?

The sub should be run about 300 times to get to the end of my VBA code, but it stalls at random instances of running that sub. There is no pattern. I suppose it is the server behind the URLs used that is unstable.

What should I do to get the code to just not insert the pic if something goes wrong?

BW Peter Bunde Hansen


Code:
Sub GetShapeFromWeb(strShpUrl As String, rngTarget As Range)
    Dim Shp As Shape
    
    On Error GoTo ErrHandler
    
    Debug.Print "Inserting pic from URL " & strShpUrl
    
    If strShpUrl <> "" Then
 
    With rngTarget
        With .Parent
            .Pictures.Insert strShpUrl
            Set Shp = .Shapes(.Shapes.Count)
        End With
        Shp.Left = .Left
        Shp.Top = .Top
    End With
    
    Debug.Print "Inserted pic from URL " & strShpUrl
    
    Shp.Select
    
    FitPic
 
    Set Shp = Nothing
    
    End If
    
Abort_insert_pic:
    
Exit Sub
    
ErrHandler:
    
    
    Debug.Print "GetShapeFromWeb: " & strShpUrl & " - " & rngTarget
    
    Resume Abort_insert_pic
End Sub
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Update: my theory is, the server does not reply, and then my code of cause stalls. Is there a way to set a timeout or else, so that the VBA code errors instead of just stopping?
 
Upvote 0
You could use the Windows API function URLDownloadToFile to download the picture file to a local folder, then specify that local file as the Pictures.Insert argument.
 
Upvote 0
You could use the Windows API function URLDownloadToFile to download the picture file to a local folder, then specify that local file as the Pictures.Insert argument.
Hi John thanks for answer

I would prefer to keep everything within the Excel sheet. A lot of people will be using this sheet and I dont want to save anything on their PC and I cannot assume they have access to our intranet. Possibly, I can insert it on a hidden page, then copy it into the desired position. Any other suggestion?

I did actually find a (poor) workaround to the VBA code stalling, and that was to have the code save the sheet every time it had inserted a picture. Apparnetly, saving the sheet makes it more stable. But it takes a lot of time. I had this instability problem for months now, it must be a Microsoft bug somewhere.

BW Peter Bunde Hansen
 
Upvote 0
In that case download, insert and delete the file.

Edit - that probably won't work because the picture object must point to a file which exists.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,200
Members
453,022
Latest member
RobertV1609

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top