'Deze Sub plaatst een foto in de cel die als parameter doorgegeven wordt. De
'tweede parameter is de FullPath-naam van de foto.
Sub InsertPicture(ByVal myCell As Range, ByVal cPicture As String)
Dim mySheet As Worksheet
Dim myPicture As Shape
Dim nTop As Double
Dim nLeft As Double
Dim ScaleWidth As Double
Dim ScaleHeight As Double
Dim i As Long
'De coördinaten van de linker bovenhoek van de foto en die van de cel zijn gelijk.
With myCell
nTop = .Top
nLeft = .Left
End With
'De worksheet waartoe de cel behoort, moeten we kennen.
Set mySheet = myCell.Parent
'Veronderstel dat de naam van het fotobestand OK is, maar het formaat niet. Dan zal de
'AddPicture methode een foutmelding veroorzaken.
On Error GoTo Errhandler
'Een nieuwe foto (Shape) toevoegen op de gepaste coördinaten. De syntax is:
'expression.AddPicture(FileName, LinkToFile, SaveWithDocument, Left, Top, Width, Height)
'Om het Excel-bestand niet op te blazen gebruiken we de optie LinkToFile = True.
Set myPicture = mySheet.Shapes.AddPicture(cPicture, msoTrue, msoFalse, -1, -1, -1, -1)
myPicture.ScaleHeight 1, msoTrue
myPicture.ScaleWidth 1, msoTrue
' adjust cell width
ScaleWidth = (MAXROWW * scaleW) / myPicture.Width
ScaleHeight = (MAXROWH * scaleH) / myPicture.Height
If ScaleWidth < ScaleHeight Then
If ScaleWidth > 0 Then
myPicture.ScaleHeight ScaleWidth, msoFalse, msoScaleFromTopLeft
End If
myCell.ColumnWidth = MAXROWW
myCell.RowHeight = MAXROWH
Else
If ScaleHeight > 0 Then
myPicture.ScaleHeight ScaleHeight, msoFalse, msoScaleFromTopLeft
End If
myCell.RowHeight = MAXROWH
myCell.ColumnWidth = MAXROWW
End If
'center the picture
myPicture.Left = myCell.Left + ((myCell.ColumnWidth * scaleW - myPicture.Width) / 2) + 4
myPicture.Top = myCell.Top + ((myCell.RowHeight * scaleH - myPicture.Height) / 2) + 2
'Hyperlink toevoegen om de originele foto te openen als men op de "Thumbnail" klikt.
mySheet.Hyperlinks.Add Anchor:=myPicture, Address:=cPicture
Exit Sub
Errhandler:
myCell.Value = "N/A Picture"
Application.ScreenUpdating = True
End Sub