selectively deleting slides with vbscript

G

Guest

All,

I've got a PPT in which each slide has a tag that includes a "section" name.
I'd like to write a vbscript that deletes every slide in the deck where the
section tag equals a certain value.

I've tried this:

For Each oSlide In oPres.Slides
If oSlide.Tags("Section") = "SomeValue" Then oSlide.Delete
Next

But it doesn't work as expected, probably because each time you delete a
slide, then the Slides collection changes, which screws up the loop.

Is there a better way to accomplish this?

Cheers,
Matt Stuehler
 
S

Shyam Pillai

Use a For-Next loop instead counting down the slide numbers.

For I= oPres.Slides.Count to 1 Step -1
'......
'......
Next
 
D

David M. Marcovitz

Just to elaborate on Shyam's solution (which is exactly what you want),
when you use the For Each loop you are going in order. You look at slides
1, 2, 3, 4, 5, 6, 7, ... You get to, for example, slide 3 and find that
it must be deleted. A normal person would say, "Great, now let's continue
with slide 4." However, since you have deleted slide 3, all the slides
shift down, so slide 4 is now the new slide 3. With this shifting
PowerPoint gets confused.

By counting backwards from the end, the only slides that get shifted, are
slides you have already looked at. That is, you have slides 7, 6, 5, 4,
3, 2, 1. If you look at slide 6 and find you have to delete it, slide 7
becomes slide 6, but you have already seen slide 7/6 so that is not a
problem, and you continue on to slide 5.

--David

--
David M. Marcovitz
Microsoft PowerPoint MVP
Director of Graduate Programs in Educational Technology
Loyola College in Maryland
Author of _Powerful PowerPoint for Educators_
http://www.PowerfulPowerPoint.com/
 
G

Guest

Shyam and David,

I just wanted to thank you for this expert answer and explanation - it
solved my problem perfectly, and the explanation was very helpful.

Cheers,
Matt
 

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