looping through chartobjects returns deleted charts also

R

Rolf Barbakken

What I want to do is to export all charts in my workbook as gifs. This works
fine, normally, but if I delete a chart, it will still be exported! The
exported gif is then empty. No image.

Here is the routine:
Dim wks As Worksheet
Dim cht As ChartObject
Dim strImage As String
Dim strPath As String
Dim objFSO As New FileSystemObject

For Each wks In ActiveWorkbook.Worksheets
wks.Activate
For Each cht In ActiveSheet.ChartObjects
strPath = objFSO.GetSpecialFolder(TemporaryFolder) & "\" &
cht.Name & ".gif"
strImage = strPath
gstrImage = strImage
cht.Chart.Export strImage, "GIF"
Next cht
Next wks

I have tried to find a way to detect if the chart is previously deleted,
because it seems Excel keeps the charts in the collection even if its
deleted, but the deleted charts does not show in the Worksheet.

How do I do this so that the deleted charts will not be exported?
 
T

Tom Ogilvy

Deleted charts are not retained in the ChartObjects collection to the best
of my knowledge. How are you deleting your charts.

Why do you get the Temporary Folder within the loop. why not get it once
outside the loop and be done with it.
 
R

Rolf Barbakken

I saw that thing with the tempfolder after I posted, but it was not
important for the real problem.

The users delete their chart the normal user way: they select the chart and
press "Delete". I notice that in the loop through the collection the
index-numbers increase if you delete and insert new charts - i.e. the
collection length increases and loop takes more time.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top