Inserting and resizing user defined images
Greetings all,
I am trying to have a user select a picture and input it into a range. I have come across the following problems. 1.) The aspect ratio stays the same and I want it to stretch the range. 2.) not all pictures are imported. You go through the process and nothing happens. (they are in the correct format). What am I missing here? I'm new at this so forgive me if its obvious.
The code is as follows:
Code:
sub InsertPictureInRange1()
Dim picToOpen As String
picToOpen = Application _
.GetOpenFilename("Image Files (*.jpg), *.jpg, (*.png), *.png")
If picToOpen <> "" Then InsertPictureInRange picToOpen, _
Range("d59:f68")
End Sub
Sub InsertPictureInRange(PictureFileName As String, TargetCells As Range)
' inserts a picture and resizes it to fit the TargetCells range
Dim p As Object, t As Double, l As Double, w As Double, h As Double
If TypeName(ActiveSheet) <> "Worksheet" Then Exit Sub
If Dir(PictureFileName) = "" Then Exit Sub
' import picture
Set p = ActiveSheet.Pictures.Insert(PictureFileName)
' determine positions
With TargetCells
t = Cells(58, .Columns.Count).Top
l = Cells(.Rows.Count, .Columns.Count).Left
w = .Offset(0, .Columns.Count).Left - .Left
h = .Offset(.Rows.Count, 0).Top - .Top
End With
' position picture
With p
.Top = t
.Left = l
.Width = w
.Height = h
End With
Set p = Nothing
End Sub
Re: Inserting and resizing user defined images
This is in excel 2007 by the way. Sorry I forgot to put it in the title.