Copy image from sheet to folder

nd0911

Board Regular
Joined
Jan 1, 2014
Messages
166
Is it possible to copy an image from excel sheet to folder with VBA ?

If so... In sheet1 I have an image name "Img1" and I want to copy it to the desktop.
 
Last edited:

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
The following code uses a function to export the worksheet image. Simply pass the name of the desired path, filename, and image to the function.

VBA Code:
Option Explicit

Sub test()

    Dim errorMessage As String
    
    If Not ExportImage("c:\users\domenic\desktop\", "Image1.jpg", Worksheets("Sheet1").Shapes("Image1"), errorMessage) Then
        MsgBox errorMessage, vbCritical, "Error"
        Exit Sub
    End If
    
    MsgBox "Image exported successfully!", vbExclamation
    
End Sub

Function ExportImage(ByVal saveToFolder As String, ByVal saveAsFilename As String, ByVal shapeToExport As Shape, ByRef errorMessage As String) As Boolean

    On Error GoTo errorHandler
    
    If Right(saveToFolder, 1) <> "\" Then
        saveToFolder = saveToFolder & "\"
    End If
    
    shapeToExport.Copy
    
    Dim ws As Worksheet
    Set ws = shapeToExport.Parent
    
    With ws.ChartObjects.Add(Left:=0, Top:=0, Width:=shapeToExport.Width, Height:=shapeToExport.Height)
        .Activate
        With .Chart
            .ChartArea.Format.Line.Visible = msoFalse
            .Paste
            .Export Filename:=saveToFolder & saveAsFilename
        End With
        .Delete
    End With
    
    ExportImage = True
    
    Exit Function
    
errorHandler:
    errorMessage = "Error " & Err.Number & ":" & vbCrLf & vbCrLf & Err.Description
    ExportImage = False
    
End Function

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,214,808
Messages
6,121,686
Members
449,048
Latest member
81jamesacct

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