1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| Option Explicit
Sub TesterLInsertImage2()
Dim RepertoireImage As String
Dim NomDeLImage As String
Dim ImageLargeur As Single
Dim ShImage As Worksheet
With Sheets("Prog")
RepertoireImage = .Range("H1") ' Ne contient que le nom du répertoire
NomDeLImage = .Range("I1") ' Je dissocie le nom de l'image du chemin complet
End With
Set ShImage = Sheets("Gabari recto")
With ShImage
ImageLargeur = .Range("C7").Width ' Pour fixer la largeur de l'image à la largeur de la colonne C
' ImageRatio est une fonction calculant la proportion Largeur / Hauteur pour respecter le format Paysage ou Portrait
Insert_Image2 ShImage, ShImage.Range("C7"), RepertoireImage, NomDeLImage, ImageLargeur, ImageRatio(RepertoireImage & "\" & NomDeLImage)
End With
Set ShImage = Nothing
End Sub
Sub Insert_Image2(ByVal FeuilleImage As Worksheet, ByVal CelluleImage As Range, ByVal RepertoireImages As String, ByVal NomDuFichierImage As String, ByVal LargeurImage As Single, ByVal RatioImage As Single)
Dim MonImage As Shape
With FeuilleImage
' Suppression de l'image existante
'---------------------------------
For Each MonImage In .Shapes
If MonImage.Name = "ImageFeuille" Then MonImage.Delete
Next MonImage
' Insertion de l'image
'---------------------
Set MonImage = .Shapes.AddShape(msoShapeRectangle, CelluleImage.Left, CelluleImage.Top, LargeurImage, LargeurImage / RatioImage)
With MonImage
.Name = "ImageFeuille"
With .Fill
.Visible = msoTrue
.UserPicture RepertoireImages & "\" & NomDuFichierImage
End With
With .Line
.Visible = msoTrue
.Weight = 1
End With
End With
Set MonImage = Nothing
End With
End Sub
Function ImageRatio(ByVal CheminEtNomDeLImage As String) As Single
' A partir du tuto "Utiliser la librairie Windows Image Acquisition en VBA" de SilkyRoad et Bbil
Dim Img As WIA.ImageFile
Set Img = CreateObject("WIA.ImageFile")
With Img
.LoadFile CheminEtNomDeLImage
ImageRatio = .Width / .Height
End With
Set Img = Nothing
End Function |
Partager