2

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?

Community
  • 1
  • 1
Maldred
  • 1,074
  • 4
  • 11
  • 33
  • 2
    https://stackoverflow.com/questions/17110425/vba-to-insert-embedded-picture-excel – braX Nov 20 '17 at 17:04
  • @braX That seems to work find and dandy with an already known filepath, but when requesting the user to select an item, how would I do that? – Maldred Nov 20 '17 at 17:16

1 Answers1

1

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

mooseman
  • 1,997
  • 2
  • 17
  • 29