Add multiple pictures from local directory

Leonvl

New Member
Joined
Apr 26, 2016
Messages
20
I uded the below code to insert pictures based on a URL on the web based on a cell value which worked fine.
Now I would like to insert pictures for a range from a local folder, but that apparently doesnt work with the Parent.Pictures.Insert method. Played a bit with the Shapes.AddPicture method but cant get it working. Any thoughts here? Thanks

Code:
Sub InsImg()

Dim URL As Range
    
    For Each URL In Worksheets("Sheet1").Range("A4:ZZ4")
    With URL.Parent.Pictures.Insert(URL.Value)
        On Error Resume Next
        .Left = URL.Offset(0, 0).Left + 1
        .Top = URL.Offset(0, 0).Top
        .Height = 250
'        .Width = 420
        URL.EntireRow.RowHeight = .Height
    End With
    Next
End Sub
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
How could I prevent that the image will not be wider than 580 while remaining the aspect ratio?
So if width devided by height > 2.32 than widt should be set at 580 (rather then setting height at 250).

The ideal however would be if the image could be cropped at 580 x 250....
 
Upvote 0
Try...

Code:
Option Explicit

Sub InsImg()


    Dim oShape As Shape
    Dim rCell As Range
    Dim sFullName As String
    
    With Worksheets("Sheet1")
        For Each rCell In .Range("A4:ZZ4")
            sFullName = rCell.Value
            If Len(rCell) > 0 Then
                If Len(Dir(sFullName, vbNormal)) > 0 Then
                    Set oShape = .Shapes.AddPicture( _
                        Filename:=sFullName, _
                        LinkToFile:=msoFalse, _
                        SaveWithDocument:=msoTrue, _
                        Left:=rCell.Left, _
                        Top:=rCell.Top, _
                        Height:=-1, _
                        Width:=-1)
                    With oShape
                        If .Width / .Height > 2.32 Then
                            .Width = 580
                        Else
                            .Height = 250
                        End If
                        rCell.EntireRow.RowHeight = .Height
                    End With
                End If
            End If
        Next rCell
    End With
    
End Sub

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,173
Members
451,543
Latest member
cesymcox

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