Save picture macro

doriannjeshi

Active Member
Joined
Apr 5, 2015
Messages
338
Office Version
  1. 365
Platform
  1. Windows
Hello,

I need a macro to save the pics in a column with the adjacent cell name in a specified folder locally

picture is in column A but is not showing here

Book16
AB
11231
21232
Sheet2
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Did you move your wb out of the XLStart folder and place it in another folder in your documents or "C" drive? Dave
 
Upvote 0
doriannjeshi, post #26 code was posted in error (messed up the edit). Post #27 is the new code. Dave
 
Upvote 0
@doriannjeshi
I don't have a clue what you're doing because all works as advertised on this end.
Have you changed the references to what they should be changed to?
 
Upvote 0
It's raining cats and dogs here so can't go outside to clean up!!
Tried both my suggestion and Dave's (NdNoviceHlp) with 50 pictures of varying sizes (KB wise and Height & Width wise).
The code from Dave took 0.35 seconds while mine took 13 seconds.
No additional comments required with this kind of comparison.
Thanks Dave.
 
Upvote 0
The figure from Dave's code in above post is wrong. Neglected to delete previously made files so it appeared as if the code had made them.
Have not been able to get any result since.
 
Upvote 0
"Have not been able to get any result since" Hmmm. Puzzling as it work every time with my limited testing. It occurred to me that the pics may not exactly fit the cells so it would be better to simply use the pic/shape size to make the pic. Maybe this is complete. Dave
Code:
Sub test()
'pics in Sheet2 "A"; file name in sheet2 "B"
Dim MyChart As Chart
Dim sh As Shape
On Error GoTo below
Application.ScreenUpdating = False
'add temp chart
Charts.Add.Location Where:=xlLocationAsObject, Name:="Sheet2"
Sheets("Sheet2").ChartObjects(Sheets("Sheet2").ChartObjects.Count).Name = "MYChart"
With Sheets("Sheet2")
For Each sh In .Shapes
If sh.Type = msoPicture Then
'make jpg files. Pics in any column with file name to right column
Call CreateJPG(sh.Name, CStr(.Range(sh.TopLeftCell.Address).Offset(, 1)), sh)
End If
Next sh
End With
'remove temp chart
Sheets("Sheet2").ChartObjects("MYChart").Delete
below:
Application.ScreenUpdating = True
If Err.Number <> 0 Then
MsgBox "Error"
End If
End Sub

Sub CreateJPG(PicName As String, FileNm As String, Shp As Shape)
'make image files
'picname is XL picture name; FileNm is name of file; Shp is pic in cell
Dim xRgPic As Shape
ThisWorkbook.Worksheets("sheet2").Activate
Set xRgPic = ThisWorkbook.Worksheets("sheet2").Shapes(PicName)
xRgPic.CopyPicture
'delete prev pic
With Sheets("Sheet2").ChartObjects("MYChart").Chart
    Do While .Shapes.Count > 0
        .Shapes(1).Delete
    Loop
'size chart to Rng cell
.Parent.Height = Shp.Height
.Parent.Width = Shp.Width
.Parent.Top = Shp.Top
.Parent.Left = Shp.Left
End With
'make file in wb path
With Sheets("Sheet2").ChartObjects("MYChart")
.Activate
.Chart.Paste
.Chart.Export ThisWorkbook.Path & "\" & ValidFilePath(FileNm) & ".jpg", "JPG"
End With
End Sub

Public Function ValidFilePath(Arg As String) As String
Dim RegEx As Object
Set RegEx = CreateObject("VBScript.RegExp")
With RegEx
    .Pattern = "[\\/:\*\?""<>\|]"
    .Global = True
    ValidFilePath = .Replace(Arg, "_")
End With
Set RegEx = Nothing
End Function
To operate, run the Test sub.
ps. @doriannjeshi to trial jolivanes code you will need to change this line...
Code:
Set sh1 = Sheets("Sheet2")
 
Upvote 0
Solution
This works Dave thank you, I saved the macro on a wb not personal wb. But for some images that I have in another excel file that interest me I copy them to this wb and though I change the size etc wont work!
 
Upvote 0
Nice that it works. As far as the pics copied that don't work, is a file created with the wrong size pic or no file?
Maybe add this msgbox line of code below the existing line of code and trial this...
Code:
For Each sh In .Shapes
Msgbox sh.type
The message box should be 13 for all pics. Dave
 
Upvote 0
The pics that dont work wont save
the msgbox line I don't where exactly to place it, I tried in the end of the whole macro and a couple of other places but it wont show a message
 
Upvote 0

Forum statistics

Threads
1,224,833
Messages
6,181,240
Members
453,026
Latest member
cknader

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