Save image in excel using VBA

Viewed 217

I have a situation at work where people have to manually introduce pictures in a certain page of excel and resize it also manually. As a complete very beginner I've managed to find some VBA code to help introduce the picture by clicking a button and inserting it in a certain range of cells. The problem that I have is that I cannot figure out (after searching many posts) how to correctly introduce the function to save the image without making a link to it so others can see the report without getting an error that the picture doesn't exist.

Can you kindly help me and complete where the function should be introduced?

Private Sub CommandButton3_Click()
Dim strFileName As String
Dim objPic As Picture
Dim rngDest As Range
strFileName = Application.GetOpenFilename( _
    FileFilter:="Images (*.jpg;*.gif;*.png),*.jpg;*.gif;*.png", _
    Title:="Please select an image...")
If strFileName = "False" Then Exit Sub
Set rngDest = Me.Range("B24:C26")
Set objPic = Me.Pictures.Insert(strFileName)
With objPic
    .ShapeRange.LockAspectRatio = msoFalse
    .Left = rngDest.Left
    .Top = rngDest.Top
    .Width = rngDest.Width
    .Height = rngDest.Height
End With
End Sub

Thanks in advance!

1 Answers

Try this:

Private Sub CommandButton3_Click()
    Dim strFileName As String
    Dim objPic As Shape '<<<
    Dim rngDest As Range
    strFileName = Application.GetOpenFilename( _
        FileFilter:="Images (*.jpg;*.gif;*.png),*.jpg;*.gif;*.png", _
        Title:="Please select an image...")
    If strFileName = "False" Then Exit Sub
    Set rngDest = Me.Range("B24:C26")

     Set objPic = Me.Shapes.AddPicture(Filename:=strFileName, _
                                       linktofile:=msoFalse, _
                                       savewithdocument:=msoCTrue, _
                                       Left:=rngDest.Left, Top:=rngDest.Top, _
                                       Width:=rngDest.Width, Height:=rngDest.Height)

End Sub
Related