Excel – How to export all images from Excel and give a name from a related cell

imagesmicrosoft excelvba

I've got an excel-sheet with about 200 rows. There are 200 images and 200 names. I have to extract every image and give the related name.

The structure is like this:

Image -> A2
Name -> B3
Image -> A5
Name -> B6
Image -> A8
Name -> B9

The image file ending doesn't matter…

How do I extract every image and give the proper name?

Best Answer

  • There's no easy way to save images from Excel, but PowerPoint has a handy Shape.Export method we can use. This macro should be used in your Excel file with all the images.

    It saves all the images on Sheet1, assuming that their filename is one cell down and to the right from the top left of the image. Make sure to edit destFolder on the first line to the correct location. It overwrites any existing files without asking, so be careful.

    Sub SaveImages()
        'the location to save all the images
        Const destFolder$ = "C:\users\...\desktop\"
        Dim ws As Worksheet
        Set ws = ThisWorkbook.Worksheets("sheet1")
        Dim ppt As Object, ps As Variant, slide As Variant
        Set ppt = CreateObject("PowerPoint.application")
        Set ps = ppt.presentations.Add
        Set slide = ps.slides.Add(1, 1)
        Dim shp As Shape, shpName$
        For Each shp In ws.Shapes
            shpName = destFolder & shp.TopLeftCell.Offset(1, 1) & ".png"
            With slide
                .Shapes(.Shapes.Count).Export shpName, 2
            End With
        Next shp
        With ps
            .Saved = True
        End With
        Set ppt = Nothing
    End Sub
  • Related Question