Sub test()
'pics in Sheet1. File names 2 columns to right of pic
Dim MyChart As Chart
Dim sh As Shape
On Error GoTo below
Application.ScreenUpdating = False
'add temp chart
Charts.Add.Location Where:=xlLocationAsObject, Name:="Sheet1"
Sheets("Sheet1").ChartObjects(Sheets("Sheet1").ChartObjects.Count).Name = "MYChart"
With Sheets("Sheet1")
For Each sh In .Shapes
If sh.Type = msoPicture Or sh.Type = msoLinkedPicture Then
'****make jpg files. Pics in any column with file name 2 columns to right
Call CreateJPG(sh.Name, CStr(.Range(sh.TopLeftCell.Address).Offset(, 2)), sh)
End If
Next sh
End With
'remove temp chart
Sheets("Sheet1").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, FolderLoc As String
'****adjust folderpath to suit
FolderLoc = "C:\testfolder\"
ThisWorkbook.Worksheets("sheet1").Activate
Set xRgPic = ThisWorkbook.Worksheets("sheet1").Shapes(PicName)
xRgPic.CopyPicture
'size chart to Rng cell
With Sheets("Sheet1").ChartObjects("MYChart").Chart
.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("Sheet1").ChartObjects("MYChart")
.Activate
.Chart.Paste
.Chart.Export FolderLoc & 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