Click here to Skip to main content
15,888,816 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I created a VB.net app several years ago that automatically inserts pictures into different Excel cells. After inserting, it also fits the picture to the cell and then centers the picture in the cell. Everything has been working fine until we upgraded to Excel 2010. Now when we run the program, certain picture types no longer get scaled properly. Here is an example of my code:

VB
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


I have access to another computer that still has Excel 2007 so I ran some tests that compared my variable outputs between 2007 and 2010. Everything was the same except StartingHeight and StartingWidth which come from the shaperange height and width properties. I don't understand why Excel is now calculating these properties differently? In some cases the property values are not even close between Excel versions. In one case, Excel 2007 had the height at 1584 and Excel 2010 had the height at 508.75.

I have been playing around with using Shapes.AddPicture rather then pictures.insert. The Shapes.AddPicture routine requires the picture size when you make the call to AddPicture. If I put the correct size of my picture cell in the call, it will fit the picture but it also stretches and distorts the picture from the original ratio. I do not want that so that method won't work for me. Anyone have any other ideas?
Posted
Comments
CHill60 17-Nov-15 12:13pm    
Your code appeared to work for me (once I'd added a workbook, activated it and the 1st sheet before selecting a range).
Have you imported the right reference to Microsoft.Office.Interop?
theskiguy 17-Nov-15 15:13pm    
I am using the Microsoft Excel 14.0 Object library which I believe is correct for Excel 2010? I have also found that my existing code will size and position certain pictures correctly but not others. In Excel 2007 it worked all the time. Don't know what is going on.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900