none
Question about inserting images into excel RRS feed

  • Question

  • Hi everyone, how is everyone doing? I have small question

    I'm currently inserting images using the below code: 

    ws.Shapes.AddPicture(imgPath, MsoTriState.msoFalse, MsoTriState.msoCTrue, 55, 55, 300, 300);

    Problem with that is, I Dont want to specify the scale (Height, Width) via code, Is it possible for the image to be inserted in its original Scale? just as you would if you drag and drop an image into word doc, and use Insert > Add pictures manually...

    Thank you.

    Friday, July 5, 2019 5:33 PM

All replies

  • The following code example inserts a picture with current dimensions

    Sub Macro1()
        'Insert picture and set width of picture
        Dim shpPic As Shape
        Dim strPathAndFile As String
        Dim lngLeft As Long
        Dim lngTop As Long
       
        strPathAndFile = ThisWorkbook.Path & "\Pictures\Wooden Truck.JPG"
        lngLeft = Range("C3").Left
        lngTop = Range("C3").Top
       
        'Setting Width and Height to -1 retains picture current dimensions
        'and locks the aspect ratio.
        Set shpPic = ActiveSheet.Shapes.AddPicture(Filename:=strPathAndFile, _
                                    LinkToFile:=msoFalse, _
                                    SaveWithDocument:=msoCTrue, _
                                    Left:=lngLeft, _
                                    Top:=lngTop, _
                                    Width:=-1, _
                                    Height:=-1)
                                   
        With shpPic
            .LockAspectRatio = True 'Can be True or false
            .Width = 50   'Setting width will also change height at ratio of height to width

            'Set .LockAspectRatio = False to change width and height without retaining the ratio.

        End With

    End Sub


    Regards, OssieMac

    Saturday, July 6, 2019 7:12 AM