I have an Excel file that contains many images inserted into cells (in column A). I try to create a macro to extract all those images, each as name of what is in column C.
The images are resized to their original size, and after the extraction restored to their reduced sizes.
I have a macro that gives me an out of memory error (Run-time error 6: Overflow), but works OK if I run it step-by-step:
Sub ImagestoFiles()
Dim xImg As Shape
Dim xStrImgName As String
Dim xHeight As Double
Dim xWidth As Double
Dim xCell As Range
For i = 2 To 100
Set xCell = Range("A" & i)
xCell.Select
Selection.PlacePictureOverCells
With ActiveSheet.Shapes(ActiveSheet.Shapes.Count)
.LockAspectRatio = True
.Top = xCell.Top
.Left = xCell.Left
.Height = xCell.RowHeight
End With
Next i
For Each xImg In ActiveSheet.Shapes
xStrImgName = xImg.TopLeftCell.Offset(0, 2).Value
xHeight = xImg.Height
xWidth = xImg.Width
If xStrImgName <> "" Then
xImg.ScaleHeight 1, msoTrue, msoScaleFromTopLeft
xImg.ScaleWidth 1, msoTrue, msoScaleFromTopLeft
xImg.Copy
Set xObjChar = ActiveSheet.ChartObjects.Add(0, 0, xImg.Width, xImg.Height)
With xObjChar
.Border.LineStyle = xlLineStyleNone
.Activate
ActiveChart.Paste
.Chart.Export xStrPath & xStrImgName & ".jpg"
.Delete
End With
xImg.Height = xHeight
xImg.Width = xWidth
End If
Next
End Sub
The error occurs in the Scale commands:
xImg.ScaleHeight 1, msoTrue, msoScaleFromTopLeft
xImg.ScaleWidth 1, msoTrue, msoScaleFromTopLeft
If it works OK when run step-by-step, why doesn't it work properly? How can it be solved?