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
| Option Explicit
Sub test2()
Dim Pict As Picture, Fichier As Variant, dp
Fichier = Application.GetOpenFilename(FileFilter:=" Image File ( *.jpg;*.png;*gif;*.wmf;*.bmp), ( *.jpg;*.png;*gif;*.wmf;*.bmp), images Files, *.*", FilterIndex:=1)
If Fichier = False Then Exit Sub 'si on annule dans la boite de dialogue
Fichier = imageminime(Fichier)
Set Pict = Sheets(2).Pictures.Insert(Fichier) 'on insert l'image tel quel
dp = Dimention_position(Range("A3:D8"), Pict, 4)
With Pict
.Name = "img1" 'je nome l'image (facultatif )mais ca peut servir pour (l'identifier/la retrouver) plus tard
.ShapeRange.LockAspectRatio = msoTrue ' lock leratio indéformable
.Top = dp(2): .Left = dp(3): .Width = dp(0): .Height = dp(1)
'.Placement = 1
End With
Set Pict = Sheets(2).Pictures.Insert(Fichier) 'on insert l'image tel quel
dp = Dimention_position(Range("F3:H28"), Pict, 4)
With Pict
.Name = "img1" 'je nome l'image (facultatif )mais ca peut servir pour (l'identifier/la retrouver) plus tard
.ShapeRange.LockAspectRatio = msoTrue ' lock leratio indéformable
.Top = dp(2): .Left = dp(3): .Width = dp(0): .Height = dp(1)
'.Placement = 1
End With
Set Pict = Sheets(2).Pictures.Insert(Fichier) 'on insert l'image tel quel
dp = Dimention_position(Range("J8:K10"), Pict, 4)
With Pict
.Name = "img1" 'je nome l'image (facultatif )mais ca peut servir pour (l'identifier/la retrouver) plus tard
.ShapeRange.LockAspectRatio = msoTrue ' lock leratio indéformable
.Top = dp(2): .Left = dp(3): .Width = dp(0): .Height = dp(1)
'.Placement = 1
End With
'Kill fichier
End Sub
'
Function Dimention_position(Rng, Pict As Picture, Optional space As Double = 0)
Dim Wr&, Hr&, W&, H&, L&, T&, Sp1&, Sp2&, ratio&
With Pict
ratio = .Width / .Height ' calcul ratio
Wr = Rng.Width: Hr = Rng.Height ' width range' height range
If (Wr / Hr < ratio) Then
'.Width = wr - space
W = Wr - space: H = .Height / (.Width / (Wr - (space / ratio)))
Else
'.Height = Hr - (space / ratio)
H = Hr - (space / ratio): W = .Width / ((.Height / (Hr - space)))
End If
L = Rng.Left + ((Wr - W) / 2): T = Rng.Top + ((Hr - H) / 2)
End With
Dimention_position = Array(W, H, T, L - Sp1)
End Function |
Partager