title: Insert picture into Excel and keep aspect ratio without exceeding dimensions with VBA
body: <p>I am exporting data from an Access database into an Excel report, and part of what needs to be included in the report are pictures corresponding to the data. The pictures are stored in a shared file and are inserted into the Excel file like so:</p>
  
  <pre><code>Dim P As Object
  Dim xlApp As Excel.Application
  Dim WB As Workbook
  
  Set xlApp = New Excel.Application
  
  With xlApp
       .Visible = False
       .DisplayAlerts = False
  End With
  
  Set WB = xlApp.Workbooks.Open(FilePath, , True)
  
  Set P = xlApp.Sheets(1).Pictures.Insert(PicPath) 'Insert picture
  With P
       With .ShapeRange
            .LockAspectRatio = msoFalse
            .Width = 375
            .Height = 260
       End With
       .Left = xlApp.Sheets(1).cells(y, x).Left
       .Top = xlApp.Sheets(1).cells(y, x).Top
       .Placement = 1
       .PrintObject = True
  End With
  
  WB.SaveAs FileName:= NewName, CreateBackup:=False 
  WB.Close SaveChanges:=True
  
  xlApp.DisplayAlerts = True
  xlApp.Application.Quit
  </code></pre>
  
  <p>The issue I am having is that I can't seem to be able to keep the aspect ratio of the pictures and make sure that at the same time they don't exceed the bounds of the space they are supposed to fit in the Excel form. The pictures are also all screenshots so there is a large amount of variability in their shape and size.                       </p>
  
  <p>Basically what I want to do is something to the effect of grabbing the corner of the picture and expanding it until it touches either the left or bottom edge of the range it is supposed to be placed in. </p>
  
  <p>This would maximize the size of the image for the space without distorting it.</p>