Export Pictures Excel VBA

前端 未结 3 1978
情歌与酒
情歌与酒 2020-12-03 16:32

I\'m having trouble trying to select and export all pictures from a workbook. I only want the pictures. I need to select and save all of them as:\"Photo 1\", \"Photo 2\", \"

3条回答
  •  情歌与酒
    2020-12-03 17:01

    This code is based on what I found here. It has been heavily modified and somewhat streamlined. This code will save all the pictures in a Workbook from all Worksheets to the same folder as the Workbook, in JPG format.

    It uses the Export() Method of the Chart object to accomplish this.

    Sub ExportAllPictures()
        Dim MyChart As Chart
        Dim n As Long, shCount As Long
        Dim Sht As Worksheet
        Dim pictureNumber As Integer
    
        Application.ScreenUpdating = False
        pictureNumber = 1
        For Each Sht In ActiveWorkbook.Sheets
            shCount = Sht.Shapes.Count
            If Not shCount > 0 Then Exit Sub
    
            For n = 1 To shCount
                If InStr(Sht.Shapes(n).Name, "Picture") > 0 Then
                    'create chart as a canvas for saving this picture
                    Set MyChart = Charts.Add
                    MyChart.Name = "TemporaryPictureChart"
                    'move chart to the sheet where the picture is
                    Set MyChart = MyChart.Location(Where:=xlLocationAsObject, Name:=Sht.Name)
    
                    'resize chart to picture size
                    MyChart.ChartArea.Width = Sht.Shapes(n).Width
                    MyChart.ChartArea.Height = Sht.Shapes(n).Height
                    MyChart.Parent.Border.LineStyle = 0 'remove shape container border
    
                    'copy picture
                    Sht.Shapes(n).Copy
    
                    'paste picture into chart
                    MyChart.ChartArea.Select
                    MyChart.Paste
    
                    'save chart as jpg
                    MyChart.Export Filename:=Sht.Parent.Path & "\Picture-" & pictureNumber & ".jpg", FilterName:="jpg"
                    pictureNumber = pictureNumber + 1
    
                    'delete chart
                    Sht.Cells(1, 1).Activate
                    Sht.ChartObjects(Sht.ChartObjects.Count).Delete
                End If
            Next
        Next Sht
        Application.ScreenUpdating = True
    End Sub
    

提交回复
热议问题