Keeping aspect ratio and orientation when inserting pictures

DasWolf60652

New Member
Joined
Jul 15, 2015
Messages
9
Hey everyone,

Rory helped me last time get the declaration correct for my function but now my colleague wants to maintain aspect ratio and orientation but still have all the pictures the same size.( either legal or landscape orientation)


I can maintain original aspect ratio by having -1 for both width and height in my Addpictures step.

But in the With shp step, I think I need to use an IF statement to have excel determine whether the width is longer than the height and then set the correct orientation size from there.

Is this possible with my current function or should I have a different function for retrieving the photo information and then divide by cell ratio?:eeek:





Code:
Function insert(PicPath, counter)   
 Dim shp                   As Shape
    With ActiveSheet
    
    
        Set shp = .Shapes.AddPicture(Filename:=PicPath, LinkToFile:=msoFalse, SaveWithDocument:=msoTrue, _
                                     Left:=Range("B" & counter).Offset(2, 0).Left, Top:=Range("B" & counter).Offset(2, 0).Top, _
                                     Width:=-1, Height:=-1)
    End With
    With shp
       
        .LockAspectRatio = msoTrue
          .Height = 210
          .Width = 150
        .Placement = 1
        
    End With


End Function
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Function insert(PicPath, counter)
Dim shp As Shape
With ActiveSheet


Set shp = .Shapes.AddPicture(Filename:=PicPath, LinkToFile:=msoFalse, SaveWithDocument:=msoTrue, _
Left:=Range("B" & counter).Offset(2, 0).Left, Top:=Range("B" & counter).Offset(2, 0).Top, _
Width:=-1, Height:=-1)
End With
With shp

.LockAspectRatio = msoTrue
.Height = 210
.Width = 150
.Placement = 1

End With


End Function
 
Upvote 0
I solved this soon after posting...


Code:
Function insert(PicPath, counter)
    Dim shp As Shape
    With ActiveSheet
        Set shp = .Shapes.AddPicture(Filename:=PicPath, LinkToFile:=msoFalse, SaveWithDocument:=msoTrue, _
                                     Left:=Range("B" & counter).Offset(2, 0).Left, Top:=Range("B" & counter).Offset(2, 0).Top, _
                                     Width:=-1, Height:=-1)
    End With
    
    With shp
     
        .Line.Weight = 2
        .Line.Visible = msoTrue
        .LockAspectRatio = msoTrue
        .Placement = xlMoveAndSize
            If .Width > .Height Then
                .Height = 202.5
                .Width = 270
                .Top = .Top + 30
                .Left = .Left + 20
            Else
                .Height = 270
                .Width = 202.5
                .Top = .Top + 10
                .Left = .Left + 50
            End If
    End With
End Function
 
Upvote 0

Forum statistics

Threads
1,222,827
Messages
6,168,482
Members
452,192
Latest member
FengXue

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