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

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
There aren't 58 lines in the code I posted. Which line is highlighted when you click Debug?

Apologies Andrew, its one of those days. Its was a ~Run Time Error "58" The line of code highlighted after the debug is:-

Name Folder & FileName & "_files\" & File As Folder & FileName & ".png"
 
Upvote 0
Maybe try:

Rich (BB 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
        If Dir(Folder & FileName & ".png") <> "" Then
            Kill Folder & FileName & ".png"
        End If
        File = Dir(Folder & FileName & "_files\" & FileName & "*.png")
        Name Folder & FileName & "_files\" & File As Folder & FileName & ".png"
        Kill Folder & FileName & ".htm"
        DoEvents
        Kill Folder & FileName & "_files\*.*"
        RmDir Folder & FileName & "_files"
    Next Pic
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Maybe try:

Rich (BB 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
        If Dir(Folder & FileName & ".png") <> "" Then
            Kill Folder & FileName & ".png"
        End If
        File = Dir(Folder & FileName & "_files\" & FileName & "*.png")
        Name Folder & FileName & "_files\" & File As Folder & FileName & ".png"
        Kill Folder & FileName & ".htm"
        DoEvents
        Kill Folder & FileName & "_files\*.*"
        RmDir Folder & FileName & "_files"
    Next Pic
    Application.ScreenUpdating = True
End Sub


That did the trick perfectly. thanks!!! Great Support as always.
 
Upvote 0
Hi

I've encountered another error when trying to run this macro to extract images from a spreadsheet.
As the macro is running during the process it has stopped and I get the runtime error "1004":
"Methid "Publish" of object "PublishObject" failed.
When I Debug it highlights the text below.

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
If Dir(Folder & FileName & ".png") <> "" Then
Kill Folder & FileName & ".png"
End If
File = Dir(Folder & FileName & "_files\" & FileName & "*.png")
Name Folder & FileName & "_files\" & File As Folder & FileName & ".png"
Kill Folder & FileName & ".htm"
DoEvents
Kill Folder & FileName & "_files\*.*"
RmDir Folder & FileName & "_files"
Next Pic
Application.ScreenUpdating = True
End Sub

Alternatively if I click "End" it opens an extra excel window and displays an image from the original sheet. With the workbook labelled as the product code.

Any ideas on the reason why this is happening or a possible solution.

Thanks
 
Upvote 0

Forum statistics

Threads
1,215,425
Messages
6,124,827
Members
449,190
Latest member
rscraig11

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