Search code examples
vbaexcelaspect-ratio

Disabling Lock Aspect Ratio VBA - Excel


I've read a few forums but none of them seem to be working for me.

I'm pulling pictures from the web, and inserting them into my spreadsheet. I'd like all of these pictures to have the same dimension.

My code is as follows:

Dim img_url as string, picture as object
img_url = Range("A1")    'Some url with an img

With ActiveSheet.Pictures
   Set Picture = ActiveSheet.Pictures.Insert(img_url)
   Picture.LockAspectRatio = msoFalse
   Picture.Width = 25
   PictureHeight = 25
End With

Every time I run it, the Lock Aspect Ratio setting is still checked, and the image is not in the square format I'm looking for.

Any advice would be much appreciated.

Thanks


Solution

  • Use the code below, the LockAspectRatio attribute is a property of the Picture.ShapeRange object, and not the Picture.

    Option Explicit
    
    Sub ImageAttributes()
    
    Dim img_url As String
    Dim picture As Object
    
    img_url = Range("A1")    'Some url with an img
    
    With ActiveSheet
       Set picture = .Pictures.Insert(img_url)
       With picture
            With .ShapeRange
              .LockAspectRatio = msoFalse
              .Width = 25
              .Height = 25
            End With
       End With
    End With
    
    
    End Sub