Inserting Picture with VBA alters aspect ratio in 2010, but not 2007

matgray87

New Member
Joined
Oct 6, 2011
Messages
1
Hi, I am using the below code to check for existing image (and delete if it exists), and then add an image into a cell range - centred and fit to size (/1.05). This works fine in Excel 2007, but in Excel 2010, the image is being squashed horizontally > < ... any ideas why?

Code:
Sub InsertPicture()
Dim sPicture As String, pic As Picture

On Error GoTo Noimage
ActiveSheet.Shapes("ProductImage").Select

If MsgBox("This will replace the existing image.", vbOKCancel + vbExclamation, "Are you sure?") = vbCancel Then Exit Sub

Selection.Delete
    
Noimage:

cell = Range("Image").Select
MyRange = Selection.Address
sPicture = Application.GetOpenFilename _
    ("Pictures (*.gif; *.jpg; *.bmp; *.tif), *.gif; *.jpg; *.bmp; *.tif", _
     , "Select Picture to Import")
 
If sPicture = "False" Then Exit Sub
 
Set pic = ActiveSheet.Pictures.Insert(sPicture)

pic.Select

Selection.Name = "ProductImage"

With pic
    .ShapeRange.LockAspectRatio = msoTrue
        If .Width > .Height Then
            .Width = Range(MyRange).Width / 1.05
            If .Height > Range(MyRange).Height Then .Height = Range(MyRange).Height / 1.05
        Else
            .Height = Range(MyRange).Height / 1.05
            If .Width > Range(MyRange).Width Then .Width = Range(MyRange).Width / 1.05
        End If
    .Top = ActiveCell.Top + ((Range(MyRange).Height - .Height) / 2)
    .Left = ActiveCell.Left + ((Range(MyRange).Width - .Width) / 2)
    .Placement = xlMoveAndSize
End With
    
Range("B27").Select
Range("B27").Value = 0
Range("D26").Select
    
Set pic = Nothing
 
End Sub

Thanks,
Matt
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college

Forum statistics

Threads
1,216,171
Messages
6,129,284
Members
449,498
Latest member
Lee_ray

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top