Have you ever wanted to use shapes or flowcharts created in Excel for presentation slides or websites? Taking screenshots and cropping them manually can be tedious. With VBA, you can instantly save specific shapes as high-quality image files to any location you choose.
In this article, I will explain this slightly advanced but reliable technique, along with how the code works.
Why Can’t We Export Directly?
First, it is important to know that VBA unfortunately does not have a simple command like Shape.ExportAsImage() to directly save a shape as an image. Therefore, we need to use a slightly creative workaround.
Using a Chart Object as a “Temporary Canvas”
Since we cannot save the shape directly, a common VBA method involves using a Chart object as a “temporary canvas.” The specific steps are as follows:
- Copy the target shape to the clipboard as an image (
.CopyPicture). - Create a temporary “blank chart (Chart object)” on the sheet.
- Paste the clipboard image into that chart (
.Paste). - Export the chart containing the pasted image as a file (
.Export). - Delete the temporary chart from the sheet (
.Delete).
By following these steps, you can indirectly save the shape as an image file.
Practical Code: Exporting a Shape as a PNG Image
This process is a bit special. Because we need a short delay between “Copy” and “Paste” to ensure the system catches up, we will implement this by splitting it into two procedures.
Preparation
Place the shape you want to export on the sheet and change its name to “ExportTargetShape”. (You can rename it by selecting the shape -> Shape Format tab -> Selection Pane).
VBA Code Example
Copy the following two procedures into a standard module.
' --- Procedure 1: Copy the shape and create a temporary chart ---
Sub ExportShapeAsImage()
' Specify the shape you want to export as an image
With ActiveSheet.Shapes("ExportTargetShape")
' (1) Copy the shape to the clipboard as a picture
.CopyPicture
' (2) Create a temporary chart object and name it
' Key point: Create it with the same size as the shape
ActiveSheet.ChartObjects.Add(0, 0, .Width, .Height).Name = "TempChartForExport"
End With
' (Important) To wait for the clipboard copy to complete,
' schedule the next procedure to run 1 second later
Application.OnTime Now + TimeValue("00:00:01"), "PasteAndExportChart"
End Sub
' --- Procedure 2: Paste to chart, export, and cleanup ---
Sub PasteAndExportChart()
' Manipulate the temporary chart object created in step (2)
With ActiveSheet.ChartObjects("TempChartForExport")
' (3) Paste the image into the chart area
.Chart.Paste
' (4) Export the chart as a PNG file
' ThisWorkbook.Path refers to the same folder as this Excel file
.Chart.Export ThisWorkbook.Path & "\ShapeImage.png"
' (5) Delete the temporary chart
.Delete
End With
MsgBox "The shape has been saved as an image.", vbInformation
End Sub
Explanation of the Code
ExportShapeAsImage Procedure
The role of this procedure is “Preparation.” It uses .CopyPicture to copy the shape as an image and .Add to create an empty chart as a temporary canvas.
The most important part is Application.OnTime. The .CopyPicture process runs asynchronously to the VBA code execution. If you try to paste immediately, the copy might not be finished, causing the process to fail. Therefore, we use OnTime to schedule the next procedure (PasteAndExportChart) to run 1 second later, ensuring the process continues safely.
PasteAndExportChart Procedure
This procedure, called 1 second later, performs the actual “Paste” and “Export.” It uses .Chart.Paste to paste the image and .Chart.Export to save it as a file. If you change the file extension to .jpg, you can save it in that format as well. Finally, since TempChartForExport is only temporary, we use .Delete to remove it and clean up the sheet.
Summary
In this article, I explained a slightly advanced technique for saving shapes as image files using VBA.
- VBA has no direct command to save a shape as an image.
- The standard solution is to use a ChartObject as a temporary canvas.
- The workflow is: Copy -> Create Temporary Chart -> (Time Delay) -> Paste -> Export -> Delete.
- Use
Application.OnTimeto ensure there is a delay between copying and pasting.
Although it looks complex at first glance, this sequence is a very versatile technique. You can use it to automate various tasks, such as generating figures for reports or mass-producing images for manuals.
