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
 
It occurred to me that when it works but nothing gets saved in the folder where the file is, that it's likely saving the file elsewhere. Maybe trial...
Code:
Msgbox ThisWorkbook.Path & "\" & FileNm & ".jpg"
.Chart.Export ThisWorkbook.Path & "\" & FileNm & ".jpg", "JPG"
or perhaps your file name is no good. You may need to add this...
Code:
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
So you would then change this line of code...
Code:
.Chart.Export ThisWorkbook.Path & "\" & ValidFilePath(FileNm) & ".jpg", "JPG"
@jolivanes no offense intended. I have not tested with 50 pics and timed it and I don't plan on doing so as I'm not that invested in this project. It just seems a lot more efficient to not be repetitively creating and deleting charts when you really only need just one. Dave
It creates a chart size image that is unfortunately skewed in proportions
I get this error
Run-time error '75':
Path/File access error
in this line
.Chart.Export ThisWorkbook.Path & "\" & ValidFilePath(FileNm) & ".jpg", "JPG"

Sub test()
'pics in Sheet2 "A"; file name in sheet2 "B"
Dim MyChart As Chart
Dim sh As Shape
'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
Call CreateJPG(sh.Name, CStr(Range(sh.TopLeftCell.Address).Offset(, 1))) '<------Removed period before Range
End If
Next sh
End With
'remove temp chart
Sheets("Sheet2").ChartObjects("MYChart").Delete
End Sub

Sub CreateJPG(PicName As String, FileNm As String)
'make image files
'picname is XL picture name; FileNm is name of file
Dim xRgPic As Shape
Sheets("Sheet2").Activate
Set xRgPic = Sheets("Sheet2").Shapes(PicName)
xRgPic.CopyPicture
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
 
Upvote 0

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
You can resize the chart like this...
Code:
Sheets("Sheet2").ChartObjects(Sheets("Sheet2").ChartObjects.Count).Name = "MYChart"
With Sheets("Sheet2").ChartObjects("MYChart").Chart
.Parent.Height = Sheets("Sheet2").Range("A1").Height
.Parent.Width = Sheets("Sheet2").Range("A1").Width
.Parent.Top = Sheets("Sheet2").Range("A1").Top
.Parent.Left = Sheets("Sheet2").Range("A1").Left
End With
Assuming that all of your pics are the same size. As far as the file path, did you trial the filepath msgbox suggestion to see what it says? Dave
 
Upvote 0
Hi Dave.
No, I am not offended. If it sounds like it, my apologies.
What you suggested makes sense of course but the only thing I did not know is the difference between what I suggested and having to resize the tempchart for every picture, which I did.
With 50 pictures and shapes (pictures) the width of two times the regular column width, it took about 3 times as long when not deleting the tempchart.
It was just a quick trial so the code might not have been up to snuff.
 
Upvote 0
You can resize the chart like this...
Code:
Sheets("Sheet2").ChartObjects(Sheets("Sheet2").ChartObjects.Count).Name = "MYChart"
With Sheets("Sheet2").ChartObjects("MYChart").Chart
.Parent.Height = Sheets("Sheet2").Range("A1").Height
.Parent.Width = Sheets("Sheet2").Range("A1").Width
.Parent.Top = Sheets("Sheet2").Range("A1").Top
.Parent.Left = Sheets("Sheet2").Range("A1").Left
End With
Assuming that all of your pics are the same size. As far as the file path, did you trial the filepath msgbox suggestion to see what it says? Dave
Pictures have different sizes I don't know where exactly to place this code.
Msgbox indicates the \XLSTART folder , maybe since I am running test macro from the visual basic.
 
Upvote 0
"\XLSTART folder " I don't think the new(maybe old?) MS security will let you save things there and that's why you had the access error after the file name was right. Put the wb with the code in any folder on your "C" drive and see what happens. The first line of the chart resize code above is posted previous. Place the code below it... I could have a been a bit more clear. I was creating and deleting charts in a somewhat similar looping routine and found that watching the task manger was interesting when U repetitively create and delete charts. Perhaps there may have been other stuff going on unrelated to the memory expansion. But changing the routine, or maybe the overall approach, seemed to have remedied the problem. Maybe coincidence. The chart can be resized to fit every shape/address if the pics are different sizes. Dave
 
Upvote 0
This seems to be completed. HTH. 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)), _
                                         .Range(sh.TopLeftCell.Address))
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, Rng As Range)
'make image files
'picname is XL picture name; FileNm is name of file; Rng is cell with pic
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 = Rng.Height
.Parent.Width = Rng.Width
.Parent.Top = Rng.Top
.Parent.Left = Rng.Left
End With
'make file in wb path
With Sheets("Sheet2").ChartObjects("MYChart")
.Activate
.Chart.Paste
.Chart.Export ThisWorkbook.Path & "\" & FileNm & ".jpg", "JPG"
End With
End Sub
To operate, Run the Test Sub
 
Upvote 0
This seems to be complete. HTH. 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)), _
                                         .Range(sh.TopLeftCell.Address))
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, Rng As Range)
'make image files
'picname is XL picture name; FileNm is name of file; Rng is cell with pic
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 = Rng.Height
.Parent.Width = Rng.Width
.Parent.Top = Rng.Top
.Parent.Left = Rng.Left
End With
'make file in wb path
With Sheets("Sheet2").ChartObjects("MYChart")
.Activate
.Chart.Paste
'.Chart.Export ThisWorkbook.Path & "\" & FileNm & ".jpg", "JPG"
.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
Edit: Seems to have posted twice. If a moderator can fix, it would be good to remove the 1st post as my edit got messed up
 
Last edited:
Upvote 0
@driannjeshi
Please don't quote every post. It just makes extra clutter that is not needed, If a reference to a post is needed, reference the post number together with the poster maybe.

The suggestion I posted previously did 50 shapes (pictures) in slightly over 4 seconds.
 
Upvote 0
Yikes!!!! That's waiting around too long for something to happen. I ran it for 3 pics and it was instant. What's the clock on your alternate routine? Dave
 
Upvote 0
@NdNoviceHlp I use the macro on post #26 , on two different machines , office 2019, 2021 , same result. A chart gets created but an Error shows without explanation
 
Upvote 0

Forum statistics

Threads
1,224,833
Messages
6,181,237
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