VBA save a single group of icons/images/shapes as a picture file.

drefiek2

Board Regular
Joined
Apr 23, 2023
Messages
59
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
Hi,
I have a sheet called "Handover" with an image of a map and many icons and shapes overlaid, all grouped together which is called "Group 26".
I have an existing working button which saves the whole sheet as a PDF to a location, but I want to add the below code in so everything is done in one button click.
The code should save the group as an image (which ever file format is easiest) into a particular location. Preferably this action should be done before the PDF save action. Overwriting of successive pictures is an essential requirement, and no overwrite warning or message boxes are required.
Any help would be appreciated.
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
First, add the the following function to your module...

VBA Code:
Function ExportShapeToImage(ByVal shapeToExport As Object, ByVal saveAsFileName As String, ByRef errorMessage As String) As Boolean

    On Error GoTo errorHandler
    
    shapeToExport.CopyPicture appearance:=xlScreen, Format:=xlPicture
    
    With shapeToExport.Parent.ChartObjects.Add(Left:=0, Top:=0, Width:=shapeToExport.Width, Height:=shapeToExport.Height)
        .Activate
        With .Chart
            .ChartArea.Format.Line.Visible = msoFalse
            .Paste
            .Export fileName:=saveAsFileName, filtername:="JPG"
        End With
        .Delete
    End With
    
    ExportShapeToImage = True
    
    Exit Function
    
errorHandler:
    errorMessage = "Error " & Err.Number & ":" & vbCrLf & vbCrLf & Err.Description
    ExportShapeToImage = False
    
End Function

Then, add the following to your code...

VBA Code:
    Dim saveAsFileName As String
    Dim targetShape As Shape
    Dim errorMessage As String
    
    saveAsFileName = "c:\users\username\desktop\sample.jpg" 'change the path and filename accordingly

    Set targetShape = ThisWorkbook.Worksheets("Handover").Shapes("Group 26")
    
    errorMessage = ""
    If Not ExportShapeToImage(targetShape, saveAsFileName, errorMessage) Then
        MsgBox errorMessage, vbCritical
        Exit Sub
    End If

Hope this helps!
 
Upvote 0
Solution
Works perfectly, thanks so much! Always a joy to see ideas working.
First, add the the following function to your module...

VBA Code:
Function ExportShapeToImage(ByVal shapeToExport As Object, ByVal saveAsFileName As String, ByRef errorMessage As String) As Boolean

    On Error GoTo errorHandler
   
    shapeToExport.CopyPicture appearance:=xlScreen, Format:=xlPicture
   
    With shapeToExport.Parent.ChartObjects.Add(Left:=0, Top:=0, Width:=shapeToExport.Width, Height:=shapeToExport.Height)
        .Activate
        With .Chart
            .ChartArea.Format.Line.Visible = msoFalse
            .Paste
            .Export fileName:=saveAsFileName, filtername:="JPG"
        End With
        .Delete
    End With
   
    ExportShapeToImage = True
   
    Exit Function
   
errorHandler:
    errorMessage = "Error " & Err.Number & ":" & vbCrLf & vbCrLf & Err.Description
    ExportShapeToImage = False
   
End Function

Then, add the following to your code...

VBA Code:
    Dim saveAsFileName As String
    Dim targetShape As Shape
    Dim errorMessage As String
   
    saveAsFileName = "c:\users\username\desktop\sample.jpg" 'change the path and filename accordingly

    Set targetShape = ThisWorkbook.Worksheets("Handover").Shapes("Group 26")
   
    errorMessage = ""
    If Not ExportShapeToImage(targetShape, saveAsFileName, errorMessage) Then
        MsgBox errorMessage, vbCritical
        Exit Sub
    End If

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,224,809
Messages
6,181,076
Members
453,020
Latest member
mattg2448

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top