Home > Back-end >  How to fetch excel images with associated name using VBA?
How to fetch excel images with associated name using VBA?

Time:11-09

Problem:

I've a .xlsx file with photos on the D column and ids of them on the C column. I would like to get all the photos in a seperate folder with their id as their name.

CodePudding user response:

After a bit a research and modifying the code I found one working:

Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Sub enregistre_image()
    Dim Pict
    Dim chrt As ChartObject
    Dim name As String
    
    For Each Pict In Worksheets("Feuil1").Pictures
        Pict.CopyPicture
        W = Pict.Width
        H = Pict.Height
        name = Range(Pict.TopLeftCell.Address).Offset(0, -1).Text
        Set chrt = ActiveSheet.ChartObjects.Add(0, 0, W, H)
        Sleep 10
        Pict.CopyPicture
        chrt.Select
        ActiveChart.Paste
        chrt.Chart.Export ThisWorkbook.Path & "\images\" & name & ".jpg", "JPG"
        chrt.Delete
    Next Pict
End Sub

The sleep of 10 ms is intended to give enough time to the clipboard to not crash (are issues without it).

  • Related