Extracting and Saving Images from Spreadsheet

RockyRobin

Board Regular
Joined
Sep 19, 2013
Messages
57
Hi

I have a spreadsheet that contains product images in cells in column A. In the matching cell in column B there are product codes. I would like to be able to "batch/bulk" save these images and label them by the corresponding product.

Is this possible. Or is there a different way around this issue.

Thanks in advance.
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Try:

Code:
Sub Test()
    Dim Folder As String
    Dim Pic As Picture
    Dim FileName As String
    Dim wb As Workbook
    Dim File As String
    Folder = ThisWorkbook.Path & Application.PathSeparator
    Application.ScreenUpdating = False
    For Each Pic In ActiveSheet.Pictures
        Set wb = Workbooks.Add
        With Pic
            FileName = .TopLeftCell.Offset(0, 1).Value
            .Copy
        End With
        ActiveSheet.Pictures.Paste
        With wb
            ActiveSheet.Name = FileName
            With .PublishObjects.Add(xlSourceSheet, Folder & FileName & ".htm" _
                , FileName, "", xlHtmlStatic, FileName, "")
                .Publish (True)
                .AutoRepublish = False
            End With
            .Close SaveChanges:=False
        End With
        File = Dir(Folder & FileName & "_files\" & FileName & "*.png")
        FileCopy Folder & FileName & "_files\" & File, Folder & File
        Kill Folder & FileName & ".htm"
        DoEvents
        Kill Folder & FileName & "_files\*.*"
        RmDir Folder & FileName & "_files"
    Next Pic
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Try:

Code:
Sub Test()
    Dim Folder As String
    Dim Pic As Picture
    Dim FileName As String
    Dim wb As Workbook
    Dim File As String
    Folder = ThisWorkbook.Path & Application.PathSeparator
    Application.ScreenUpdating = False
    For Each Pic In ActiveSheet.Pictures
        Set wb = Workbooks.Add
        With Pic
            FileName = .TopLeftCell.Offset(0, 1).Value
            .Copy
        End With
        ActiveSheet.Pictures.Paste
        With wb
            ActiveSheet.Name = FileName
            With .PublishObjects.Add(xlSourceSheet, Folder & FileName & ".htm" _
                , FileName, "", xlHtmlStatic, FileName, "")
                .Publish (True)
                .AutoRepublish = False
            End With
            .Close SaveChanges:=False
        End With
        File = Dir(Folder & FileName & "_files\" & FileName & "*.png")
        FileCopy Folder & FileName & "_files\" & File, Folder & File
        Kill Folder & FileName & ".htm"
        DoEvents
        Kill Folder & FileName & "_files\*.*"
        RmDir Folder & FileName & "_files"
    Next Pic
    Application.ScreenUpdating = True
End Sub

Works like a peach, however 2 questions.
Which part do i need to tweak to remove the _image001 when the file is saved. Ideally would like it to be saved as only the product code.
If I want the file saving as jpeg, do i just tweak the *.png to *.jpg

As usual though fantastic support and guidance.
 
Upvote 0
To remove the _image001 change:

Code:
FileCopy Folder & FileName & "_files\" & File, Folder & File

to:

Code:
Name Folder & FileName & "_files\" & File As Folder & FileName & ".png"

To save as jpg would require a different approach as in:

http://www.mrexcel.com/forum/excel-questions/86030-saving-jpg-excel.html

But I have found issues with that in Excel 2010. You could use a graphics converter to convert from png to jpg.
 
Upvote 0
To remove the _image001 change:

Code:
FileCopy Folder & FileName & "_files\" & File, Folder & File

to:

Code:
Name Folder & FileName & "_files\" & File As Folder & FileName & ".png"

To save as jpg would require a different approach as in:

http://www.mrexcel.com/forum/excel-questions/86030-saving-jpg-excel.html

But I have found issues with that in Excel 2010. You could use a graphics converter to convert from png to jpg.

Hi Andrew

Sorry for the slow response. Fantastic support and help as always. The macro works perfectly.

Thanks
 
Upvote 0
Hi

Have tried this macro a few times on a few different spreadsheets and all was working fine.
However today I have tried to run it on a different spreadsheet but I get an error message. When I debug it highlights the line below.

.Publish (TRUE)

And then stops working.

Any ideas on how to resolve this error.

Thanks in advance.
 
Upvote 0

Forum statistics

Threads
1,214,819
Messages
6,121,749
Members
449,050
Latest member
excelknuckles

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