Dim ExcelApp As Excel.Application Dim PicCoord As Object Dim PicCoordHeight As Double = 112.0# Dim PicCoordWidth As Double = 152.0# Dim PicCoordHeightBorder As Integer = 2 Dim PicCoordWidthBorder As Integer = 2 ExcelApp.Range("A10").Select() PicCoord = ExcelApp.ActiveSheet.Pictures.Insert("C:\temp\mypic.jpg") With PicCoord Dim StartingHeight As Double Dim StartingWidth As Double Dim HeightRatio As Double Dim WidthRatio As Double Dim RatioToUse As Double 'Store the starting Size StartingHeight = .shaperange.height StartingWidth = .shaperange.width 'Figure the ratio we need to resize the picture HeightRatio = (PicCoordHeight - (PicCoordHeightBorder * 2)) / StartingHeight WidthRatio = (PicCoordWidth - (PicCoordWidthBorder * 2)) / StartingWidth 'Find the smallest ratio RatioToUse = Math.Min(HeightRatio, WidthRatio) .ShapeRange.LockAspectRatio = True 'Scale the picture to fit .shapeRange.ScaleHeight(RatioToUse, True) .shapeRange.ScaleWidth(RatioToUse, True) 'Shift the picture to keep it centered .top = ExcelApp.ActiveCell.Top + (PicCoordHeight - .shaperange.height) / 2 .left = ExcelApp.ActiveCell.Left + (PicCoordWidth - .shaperange.width) / 2 End With
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)