I have a macro that moves a picture within a cell to a comment within the same cell. It does this for all pictures within the active sheet. In Excel 2013, the macro runs fine. However, in Excel 2016, if I step through the code manually, it works fine. But if I run it I get the following run-time error...
...which occurs at the following line...
When I step through it manually and it gets to the above line, the picture exists within the chart. So I don't get an error. But when I run the macro and it gets to the above line, the picture doesn't exist for some reason, hence the error. Does anyone know why this is the case? Am I overlooking something? Is this some sort of bug?
Code:
Run-time error: '424':
Object required
...which occurs at the following line...
Code:
.Chart.Pictures(1).Delete
When I step through it manually and it gets to the above line, the picture exists within the chart. So I don't get an error. But when I run the macro and it gets to the above line, the picture doesn't exist for some reason, hence the error. Does anyone know why this is the case? Am I overlooking something? Is this some sort of bug?
Code:
[COLOR=darkblue]Option[/COLOR] [COLOR=darkblue]Explicit[/COLOR]
[COLOR=darkblue]Sub[/COLOR] MovePicturesFromCellIntoComments()
[COLOR=darkblue]Dim[/COLOR] oShp [COLOR=darkblue]As[/COLOR] Shape
[COLOR=darkblue]Dim[/COLOR] sSaveAsFilename [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
[COLOR=darkblue]Const[/COLOR] ZoomFactor As [COLOR=darkblue]Double[/COLOR] = 2.5 [COLOR=green]'two and a half times larger[/COLOR]
[COLOR=darkblue]With[/COLOR] ActiveSheet.ChartObjects.Add(Left:=0, Top:=0, Width:=250, Height:=250)
.Border.LineStyle = 0
[COLOR=darkblue]For[/COLOR] [COLOR=darkblue]Each[/COLOR] oShp [COLOR=darkblue]In[/COLOR] ActiveSheet.Shapes
[COLOR=darkblue]If[/COLOR] oShp.Type = msoPicture [COLOR=darkblue]Then[/COLOR]
sSaveAsFilename = Environ("temp") & "\" & oShp.Name & ".jpg"
oShp.Copy
[COLOR=darkblue]With[/COLOR] .Chart
.ChartArea.Width = oShp.Width
.ChartArea.Height = oShp.Height
.Paste
.Export sSaveAsFilename
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
[COLOR=darkblue]With[/COLOR] oShp.TopLeftCell.AddComment
.Text Text:=""
[COLOR=darkblue]With[/COLOR] .Shape
.Width = oShp.Width * ZoomFactor
.Height = oShp.Height * ZoomFactor
.Fill.UserPicture sSaveAsFilename
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
.Chart.Pictures(1).Delete
oShp.Delete
Kill sSaveAsFilename
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
[COLOR=darkblue]Next[/COLOR] oShp
.Delete
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]