Name

graphic.LockAspectRatio [= setting]

Synopsis

True retains the aspect ratio when the height or width is set; False stretches or squashes the image to match the height or width settings. The following code demonstrates the result of both settings; notice that the image filename must be reset to restore the original proportions after resizing the image:

Sub GraphicAspectRatio(  )
    Dim gr As Graphic
    ' Add the footer image.
    ActiveSheet.PageSetup.CenterFooterPicture.Filename = _
      ThisWorkbook.Path & "\wombatright.bmp"
    ActiveSheet.PageSetup.CenterFooter = "&G"
    ' Get the graphic object.
    Set gr = ActiveSheet.PageSetup.CenterFooterPicture
    ' Squash the image (height stays the same)
    gr.LockAspectRatio = False
    gr.Height = 20
    ActiveSheet.PrintOut , , , True
    ' Restore the footer image.
    ActiveSheet.PageSetup.CenterFooterPicture.Filename = _
      ThisWorkbook.Path & "\wombatright.bmp"
    ' Scale image to this width
    gr.LockAspectRatio = True
    gr.Height = 20
    ActiveSheet.PrintOut , , , True
End Sub

Get Programming Excel with VBA and .NET now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.