Reputation: 1104
The below code I got working from some help through SO and a few other resources that I joined together. With a few minor changes I was able to use it with me spreadsheet.
Sub AddPicture(l As Long, t As Long, w As Long, h As Long, aRatio As Boolean)
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
.ButtonName = "Submit"
.Title = "Select an image file"
.Filters.Clear
.Filters.Add "All Pictures", "*.*"
If .Show = -1 Then
Dim img As Picture
Set img = ActiveSheet.Pictures.Insert(.SelectedItems(1))
If (Not aRatio) Then
img.ShapeRange.LockAspectRatio = msoFalse
Else
img.ShapeRange.LockAspectRatio = msoTrue
End If
img.left = l
img.top = t
img.width = w
img.height = h
Else
End If
End With
End Sub
One thing I noticed about this code, is that the image is being added as a Linked Image
, and this may cause some issues down the road... Is there anyway to add these as Embedded images
instead?
Upvotes: 2
Views: 237
Reputation: 2017
Add this to your with block for the file picker
FullPathName = .SelectedItems(1)
Then in brax's answer link change
Filename:="C:\test\desert.jpg"
To
Filename:=FullPathName
Upvotes: 1