Insert Image from URL - Save Image with Workbook

scwisely

New Member
Joined
Jun 21, 2019
Messages
22
Hi All,
I have the below code to insert images into my worksheet from a URL; however, it doesn't save the image with the workbook/worksheet - I need the code modified below so that it saves the actual image with the worksheet and displays it if I share the file with someone else. Help! :)

Code:
Sub URLPictureInsert()Dim Pshp As Shape
Dim xRg As Range
Dim xCol As Long
On Error Resume Next
Application.ScreenUpdating = False
Set rng = ActiveSheet.Range("D10:D27")
For Each cell In rng
filenam = cell
ActiveSheet.Pictures.Insert(filenam).Select
Set Pshp = Selection.ShapeRange.Item(1)
If Pshp Is Nothing Then GoTo lab
xCol = cell.Column + 1
Set xRg = Cells(cell.Row, xCol)
With Pshp
.LockAspectRatio = msoFalse
If .Width > xRg.Width Then .Width = xRg.Width * 2 / 3
If .Height > xRg.Height Then .Height = xRg.Height * 2 / 3
.Top = xRg.Top + (xRg.Height - .Height) / 2
.Left = xRg.Left + (xRg.Width - .Width) / 2
End With
lab:
Set Pshp = Nothing
Range("A2").Select
Next
Application.ScreenUpdating = True
End Sub
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Instead of using Pictures.Insert() to add the image to the worksheet you could try using Shapes.AddPicture.

AddPicture has 2 arguments, LinkToFile and SaveWithDocument which you can use to determine how the inserted image is to be treated.
 
Upvote 0
Thank You! I am a very new to VBA - would you be able to help me with the actual code so I can copy/paste? :)

Thanks so very much Norie!
 
Upvote 0
Perhaps something like this.
Code:
Sub URLPictureInsert()
Dim xRg As Range
Dim cell As Range
Dim rng As Range

    On Error Resume Next
    
    Application.ScreenUpdating = False
    
    Set rng = ActiveSheet.Range("D10:D27")
    
    For Each cell In rng
    
        Set xRg = cell.Offset(, 1)
        
        With xRg
            ActiveSheet.Shapes.AddPicture cell.Value, msoFalse, msoTrue, .Left, .Top, .Width * 2 / 3, .Height * 2 / 3
        End With
                
    Next cell
    
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,252
Members
449,075
Latest member
staticfluids

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