XL 2013 VBA Insérer des images d'un dossier

Jouxte

XLDnaute Occasionnel
Bonjour à toutes et tous,

J'utilise un code VBA trouvé sur Internet qui me permet d'insérer des photos dans mon fichier Excel.
Je souhaiterai conserver l'homothétie de la photo et la centrer dans la cellule. (LockAspectRatio = msoTrue)

Par avance merci pour votre aide
 

Pièces jointes

  • Classeur1.xlsm
    39.8 KB · Affichages: 7
Solution
c'est par ce que tu n'emploie pas la bonne méthode pour insérer les image
tu empoie addpicture
VB:
   Set sShape = ActiveSheet.Shapes.AddPicture(PicList(lLoop), msoFalse, msoCTrue, Rng.Left, Rng.Top, Rng.Width, Rng.Height)
donc tu les dimensionne déjà avant de passer par la fonction comment veux tu qu’après ma fonction fasse son boulot

il faut utiliser la méthode pictures.insert
Code:
 Set sShape = ActiveSheet.Pictures.Insert(PicList(lloop))
comme tu peux le voir elle n'est pas dimensionnée
c'est après que l' on passe par ma fonction
j'ai donc tout repris
VB:
Sub InserImages()
    Dim PicList(), Rng As Range, sShape As Picture
    On Error Resume Next
    PicList =...

patricktoulon

XLDnaute Barbatruc
ben c'est marrant mon code est deja intégré tout du moins une de mes anciens code dans insererphoto
ca veut dire que normalement tu a compris le principe
1667937551117.png



sinon pas la plus récente mais fonctionnelles tout de même

VB:
Sub InserImages()

Dim PicList() As Variant
Dim PicFormat As String
Dim Rng As Range
Dim sShape As Shape
On Error Resume Next
PicList = Application.GetOpenFilename(PicFormat, MultiSelect:=True)
xColIndex = Application.ActiveCell.Column
If IsArray(PicList) Then
    xRowIndex = Application.ActiveCell.Row
    For lLoop = LBound(PicList) To UBound(PicList)
        Set Rng = Cells(xRowIndex, xColIndex)
        Set sShape = ActiveSheet.Shapes.AddPicture(PicList(lLoop), msoFalse, msoCTrue, Rng.Left, Rng.Top, Rng.Width, Rng.Height)
        'Selection.ShapeRange.LockAspectRatio = msoTrue
        xRowIndex = xRowIndex + 1
    place_l_image_dans Rng, sShape
   
    Next
End If
End Sub
Sub SuppImg()
ActiveSheet.Pictures.Delete
End Sub


'patricktoulon
'version 2021 en agissant directement sur l'image
Sub place_l_image_dans(Rng As Range, Shp As Picture)
    Dim x&
    With Shp
        .ShapeRange.LockAspectRatio = msoTrue    ' met  l'aspect Ratio a true
        x = (Rng.Width / Rng.Height) < (.Width / .Height) 'comparaison des ratios
        'en fonction de x et en redimensionnant le width ou le height l'autre se redimensionne automatiquement
        If x Then .Width = Rng.Width Else .Height = Rng.Height
        .Left = Rng.Left   + ((Rng.Width - .Width) / 2)'débloquer si l'image doit etre au centre horizontalement
        .Top = Rng.Top   + ((Rng.Height - .Height) / 2)'débloquer si l'image doit etre au centre verticalement
        .Placement = 1
    End With
End Sub
 

patricktoulon

XLDnaute Barbatruc
c'est par ce que tu n'emploie pas la bonne méthode pour insérer les image
tu empoie addpicture
VB:
   Set sShape = ActiveSheet.Shapes.AddPicture(PicList(lLoop), msoFalse, msoCTrue, Rng.Left, Rng.Top, Rng.Width, Rng.Height)
donc tu les dimensionne déjà avant de passer par la fonction comment veux tu qu’après ma fonction fasse son boulot

il faut utiliser la méthode pictures.insert
Code:
 Set sShape = ActiveSheet.Pictures.Insert(PicList(lloop))
comme tu peux le voir elle n'est pas dimensionnée
c'est après que l' on passe par ma fonction
j'ai donc tout repris
VB:
Sub InserImages()
    Dim PicList(), Rng As Range, sShape As Picture
    On Error Resume Next
    PicList = Application.GetOpenFilename(PicFormat, MultiSelect:=True)
    Set Rng = Selection.Cells(1)
    If Not IsArray(pictlist) Then pictlist = Array(pictlist)
    For lloop = LBound(PicList) To UBound(PicList)
        Set sShape = ActiveSheet.Pictures.Insert(PicList(lloop))
        place_l_image_dans Rng, sShape
        Set Rng = Rng.Offset(1)
    Next
End Sub
Sub SuppImg()
    ActiveSheet.Pictures.Delete
End Sub


'patricktoulon
'version 2021 en agissant directement sur l'image
Sub place_l_image_dans(Rng As Range, Shp As Picture)
    Dim x&
    With Shp
        .ShapeRange.LockAspectRatio = msoTrue    ' met  l'aspect Ratio a true
        x = (Rng.Width / Rng.Height) < (.Width / .Height)    'comparaison des ratios
        'en fonction de x et en redimensionnant le width ou le height l'autre se redimensionne automatiquement
        If x Then .Width = Rng.Width Else .Height = Rng.Height
        .Left = Rng.Left + ((Rng.Width - .Width) / 2)
        .Top = Rng.Top + ((Rng.Height - .Height) / 2)
        .Placement = 1
    End With
End Sub

voila ;)
 

Discussions similaires

Réponses
12
Affichages
446

Statistiques des forums

Discussions
312 211
Messages
2 086 298
Membres
103 171
dernier inscrit
clemm