inserting pictures

gabethegrape

New Member
Joined
Mar 3, 2009
Messages
38
I've got the following macro to insert pictures into a worksheet. I want it to do the following:

1. Keep the picture within a maximum size parameter (206 x 206)
2. If picture height is greater than picture width, minimize picture height to 206 and keep width ratio.
3. If picture width is greater than picture height, minimize picture width to 206 and keep width ratio.

My current macro only preserves the height. It works for pictures that are taller than they are wider. If I add width parameter to the code then it compresses the picture (which I don't want).

Can anyone help me out??

Cheers,
Gabe



Sub InsertPicture2()
Dim myPicture As String
Dim pic As Picture

myPicture = Application.GetOpenFilename _
("Pictures (*.gif; *.jpg; *.bmp; *.tif),*.gif; *.jpg; *.bmp; *.tif", , "Select Picture to Import")
If myPicture = "False" Then Exit Sub

If myPicture <> "" Then
Set r = Range("c21")
Set pic = ActiveSheet.Pictures.Insert(myPicture)

With pic
.Top = r.Top
.Left = r.Left
.ShapeRange.LockAspectRatio = msoFalse
.Height = 206
.Placement = xlMoveAndSize
End With
End If
End Sub
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)

J Ericson

Board Regular
Joined
Jan 31, 2009
Messages
51
Gabe,

This might not be the exact result you want but I tweak the code posted a little. I made the assumption that if the image is smaller than 206px x 206px that you wanted to enlarge the image to the max size without exceeding 206px. If you don't want images inlarged to your maximum allowable size, you may need to nest a few more if statements to get the desired results

Code:
Sub InsertPicture2()
Dim myPicture As String
Dim pic As Picture

myPicture = Application.GetOpenFilename _
("Pictures (*.gif; *.jpg; *.bmp; *.tif),*.gif; *.jpg; *.bmp; *.tif", , "Select Picture to Import")
If myPicture = "False" Then Exit Sub

If myPicture <> "" Then
Set r = Selection    'Range("c21")
Set pic = ActiveSheet.Pictures.Insert(myPicture)


    If pic.Height > pic.Width Then
        With pic
            .Top = r.Top
            .Left = r.Left
            .ShapeRange.LockAspectRatio = msoCTrue
            .Height = 206
            .Placement = xlMoveAndSize
        End With
    ElseIf pic.Width > pic.Height Then
        With pic
            .Top = r.Top
            .Left = r.Left
            .ShapeRange.LockAspectRatio = msoCTrue
            .Width = 206
            .Placement = xlMoveAndSize
        End With
    End If

End If
End Sub
HTH,
J Ericson
 
Upvote 0

gabethegrape

New Member
Joined
Mar 3, 2009
Messages
38
Thanks, J! That worked really well. I just added one more condition for pictures of equal size:

Sub InsertPicture2()
Dim myPicture As String
Dim pic As Picture

myPicture = Application.GetOpenFilename _
("Pictures (*.gif; *.jpg; *.bmp; *.tif),*.gif; *.jpg; *.bmp; *.tif", , "Select Picture to Import")
If myPicture = "False" Then Exit Sub

If myPicture <> "" Then
Set r = Selection 'Range("c21")
Set pic = ActiveSheet.Pictures.Insert(myPicture)


If pic.Height > pic.Width Then
With pic
.Top = r.Top
.Left = r.Left
.ShapeRange.LockAspectRatio = msoCTrue
.Height = 206
.Placement = xlMoveAndSize
End With
ElseIf pic.Width > pic.Height Then
With pic
.Top = r.Top
.Left = r.Left
.ShapeRange.LockAspectRatio = msoCTrue
.Width = 206
.Placement = xlMoveAndSize
End With
ElseIf pic.Height = pic.Width Then
With pic
.Top = r.Top
.Left = r.Left
.ShapeRange.LockAspectRatio = msoCTrue
.Height = 206
.Placement = xlMoveAndSize
End With
End If

End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,191,587
Messages
5,987,512
Members
440,098
Latest member
MickyMouse123

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
Top