Save picture macro

doriannjeshi

Board Regular
Joined
Apr 5, 2015
Messages
214
Office Version
  1. 2019
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

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
- So to explain it better the picture in a1 should be saved as .jpg locally with the name of b1, and so for all the column A
 
Upvote 0
Hi doriannjeshi. If you have pictures in sheet2 "A" with picture names in corresponding "B" rows of sheet2, this seems to work. I'm not sure what you mean by save files locally so they are just saved in the same location as your wb. HTH. Dave
Code:
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)))
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
ThisWorkbook.Worksheets("sheet2").Activate
Set xRgPic = ThisWorkbook.Worksheets("sheet2").Shapes(PicName)
xRgPic.CopyPicture
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
the macro stops at:
Runtime error 9
subscript out of range
ThisWorkbook.Worksheets("sheet2").Activate
 
Upvote 0
Hi again doriannjeshi. I'm not certain what the issue is. I trialed the code successfully before posting and I just re-tried it again for the same results on office 2019 on Windows 10 Pro. Are your pictures and file names on sheet 2 as there really isn't any reason for that line to crap out. Runtime error 9 "Subscript out of range” error occurs because the object we are trying to access does not exist. Dave
 
Upvote 0
Hi, I tried on a different file and machine, same problem, it creates an empty graphic and stops. When I switched columns, it names this graphic properly but stops at the error

I tried this line too but same result
Workbooks(1).Worksheets("Sheet2").Activate
 
Upvote 0
Instead of
VBA Code:
Call CreateJPG(sh.Name, CStr(.Range(sh.TopLeftCell.Address).Offset(, 1)))
Try this:
VBA Code:
Call CreateJPG(sh.Name, CStr(Range(sh.TopLeftCell.Address).Offset(, 1))) '<------Removed period before Range
 
Upvote 0
Again error script out of range at this line
Workbooks(1).Worksheets("Sheet2").Activate
 
Upvote 0

Forum statistics

Threads
1,216,778
Messages
6,132,665
Members
449,745
Latest member
victoriar

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