View Single Post
  #10   Report Post  
Posted to microsoft.public.excel.programming
joel joel is offline
external usenet poster
 
Posts: 9,101
Default Insert picture using Macro..

Be a little bit clear about what is not working. It looks likeyou were only
croping the pictures that were found and not the default picture. I modified
the code below to fix this problem and to make the code common between the
pictures found and not found.


Sub add_pictures()

Const PictureHeight = 120
Folder = "O:\MERCHGRP\AAB\pics\Mpics\"
FName = "Picture_not_Available.jpg"
DefaultPicture = Folder & FName

Application.ScreenUpdating = False

'delete pictures
For Each shp In ActiveSheet.Shapes
If shp.Type = msoPicture Then
shp.Delete
End If
Next shp

LastCol = Cells(4, Columns.Count).End(xlToLeft).Row
Rows(9).RowHeight = PictureHeight

For Each Cell In Range("B4:IV4")
If Cell < "" Then
Cell.Offset(-3, 0).ClearContents
PictureFound = Dir(Cell.Value)
If PictureFound < "" Then

Set pict = ActiveSheet.Pictures. _
Insert(Cell.Value)
Else
Set pict = ActiveSheet.Pictures. _
Insert(DefaultPicture)
End If
pict.ShapeRange.LockAspectRatio = msoTrue
pict.ShapeRange.Height = PictureHeight
pictwidth = pict.Width
CellWidth = Cells(9, Cell.Column).Width
WidthBorder = CellWidth - pictwidth
pict.Left = Cells(9, Cell.Column).Left + (WidthBorder / 1.8)

PictHeight = pict.Height
CellHeight = Cells(9, Cell.Column).Height
HeightBorder = CellHeight - PictHeight
pict.Top = Cells(9, Cell.Column).Top + (HeightBorder / 1.8)


If pict.Width pict.Height Then
Crop = (CellWidth - pict.Width) / 2
pict.PictureFormat.CropLeft = Crop
pict.PictureFormat.CropRight = Crop
Else
Crop = (CellHeight - pict.Height) / 2
pict.PictureFormat.CropTop = Crop
pict.PictureFormat.CropBottom = Crop
End If
End If 'new line
Next Cell
Exit Sub 'new line

End Sub


" wrote:

On Jun 19, 6:59 am, Joel wrote:
I made asmall eror

from
If pict.Height 100 Then
Crop = (CellWidth - pict.Width) / 2
to
If pict.Height 100 Then
Crop = (CellHeight - pict.Height) / 2



" wrote:
On Jun 19, 3:23 am, Joel wrote:
You need to get the height and width of the cell is is going into to center
the cell


CellHeight = range("A1").Height
CellWidth = range("A1").Width


pict.LockAspectRatio = msoTrue
if pict.width pict.height then
pict.width = 100
else
pict.height = 100
end if
WidthBorder = CellWidth - pict.width
pict.left = Range("A1").left + WidthBorder/2


HeightBorder = CellHeight - pict.height
pict.top = Range("A1").Top + HeightBorder/2


" wrote:
On Jun 18, 2:07 pm, wrote:
On Jun 18, 11:20 am, Joel wrote:


You need to get the larger of the width or height variable and adjust it to 100


pict.LockAspectRatio = msoTrue
if pict.width pict.height then


else
pict.height = 100
end if


" wrote:
I have amacrothat I use toinsertpictures in excel but I am having
difficulty with thepicturesize, I would like thepictureto keep the
Aspect Ratio but to have a maximum of 100 height and a maximum 100
width is this possible, any help is appreciated.
Thanks.- Hide quoted text -


- Show quoted text -


Thanks, you are great!.
Is there any way that I can center the pictures in the cell, because
when the width is than the height it ia scaling it to 100 but then
it is being placed slightly on the cell in to the left.
thanks- Hide quoted text -


- Show quoted text -


Hi,
I am wondering if this will work! I tried but it seems that I am doing
something wrong can you help please.


pict.LockAspectRatio = msoTrue
if pict.width pict.height then
pict.cropleft = 50
pict.cropright = 50
else
pict.height = 100
end if- Hide quoted text -


- Show quoted text -


Hi,
Is it possible to Crop thepicturefrom left and right instead of
sizing it "pict.width = 100"
thanks.- Hide quoted text -


- Show quoted text -


Hi,
Thanks, but it seems that I am doing something wrong. Below is the
Complete Macro that I have know can you look and see what I am doing
wrong.


Sub add_pictures()

Const PictureHeight = 120
DefaultPicture = "O:\MERCHGRP\AAB\pics\Mpics
\Picture_not_Available.jpg"

Application.ScreenUpdating = False

'delete pictures
For Each shp In ActiveSheet.Shapes
If shp.Type = msoPicture Then
shp.Delete
End If
Next shp

LastCol = Cells(4, Columns.Count).End(xlToLeft).Row
Rows(9).RowHeight = PictureHeight

For Each Cell In Range("B4:IV4")
If Cell < "" Then
Cell.Offset(-3, 0).ClearContents
PictureFound = Dir(Cell.Value)
If PictureFound < "" Then

Set pict = ActiveSheet.Pictures. _
Insert(Cell.Value)
pict.ShapeRange.LockAspectRatio = msoTrue
pict.ShapeRange.Height = PictureHeight
pictwidth = pict.Width
CellWidth = Cells(9, Cell.Column).Width
WidthBorder = CellWidth - pictwidth
pict.Left = Cells(9, Cell.Column).Left + (WidthBorder / 1.8)

PictHeight = pict.Height
CellHeight = Cells(9, Cell.Column).Height
HeightBorder = CellHeight - PictHeight
pict.Top = Cells(9, Cell.Column).Top + (HeightBorder / 1.8)


If pict.Width pict.Height Then
Crop = (CellWidth - pict.Width) / 2
pict.PictureFormat.CropLeft = Crop
pict.PictureFormat.CropRight = Crop
End If

If pict.Height pict.Width Then
Crop = (CellHeight - pict.Height) / 2
pict.PictureFormat.CropTop = Crop
pict.PictureFormat.CropBottom = Crop
End If



Else
Set pict = ActiveSheet.Pictures. _
Insert(DefaultPicture)
pict.ShapeRange.LockAspectRatio = msoTrue
pict.ShapeRange.Height = PictureHeight
pictwidth = pict.Width
CellWidth = Cells(9, Cell.Column).Width
WidthBorder = CellWidth - pictwidth
pict.Left = Cells(9, Cell.Column).Left + (WidthBorder / 1.8)

PictHeight = pict.Height
CellHeight = Cells(9, Cell.Column).Height
HeightBorder = CellHeight - PictHeight
pict.Top = Cells(9, Cell.Column).Top + (HeightBorder / 1.8)

End If
End If 'new line
Next Cell
Exit Sub 'new line


End Sub