I don't understand: VBA acting strangely.

G

Guest

So here I am again.

This is part of masterlist:
ActivePresentation.Slides(1).Tags.Add Name:="IncludeIn", Value:="ABCDEFGHIJ"
ActivePresentation.Slides(2).Tags.Add Name:="IncludeIn", Value:="B"
ActivePresentation.Slides(3).Tags.Add Name:="IncludeIn", Value:="C"
ActivePresentation.Slides(4).Tags.Add Name:="IncludeIn", Value:="E"
ActivePresentation.Slides(5).Tags.Add Name:="IncludeIn", Value:="H"
ActivePresentation.Slides(6).Tags.Add Name:="IncludeIn", Value:="B"
ActivePresentation.Slides(7).Tags.Add Name:="IncludeIn", Value:="E"
ActivePresentation.Slides(8).Tags.Add Name:="IncludeIn", Value:="H"
ActivePresentation.Slides(9).Tags.Add Name:="IncludeIn", Value:="I"
ActivePresentation.Slides(10).Tags.Add Name:="IncludeIn", Value:="J"
ActivePresentation.Slides(11).Tags.Add Name:="IncludeIn", Value:="C"
ActivePresentation.Slides(12).Tags.Add Name:="IncludeIn", Value:="ABCDEFGHIJ"
ActivePresentation.Slides(13).Tags.Add Name:="IncludeIn", Value:="ABCJ"


Set oMasterPres = Presentations.Open("C:\Project\employee_edu.ppt")
With Application.ActivePresentation
.SaveAs "temp.ppt"
End With
' Open the MasterPresentation
masterlist
Dim oNewPres As Presentation
Dim oNewSld As Slide
Dim oMasterSld As Slide
'Dim sSlideType As String
' Create a new presentation
'Set oNewPres = Presentations.Add
For Each oMasterSld In oMasterPres.Slides
If InStr(oMasterSld.Tags("IncludeIn"), "A") > 0 Then
Else
oMasterSld.Delete
End If
Next
' Master Slide
' close the master presentation
oMasterPres.Close

Here's the odd thing I don't understand:

It's deleting all of the correct ones except about 6 slides, that are coded
the same as the others, so I don't get it:

It deletes 2 through 11, but it also deletes 12 and 13, which have "A" in
them, but it doesn't delete "1", which has the same tag as 12 and 13 -- it
should KEEP 12 and 13, but doesn't.

Any ideas?
 
C

Chirag

The following loop in your code is problematic:

For Each oMasterSld In oMasterPres.Slides
If InStr(oMasterSld.Tags("IncludeIn"), "A") > 0 Then
Else
oMasterSld.Delete
End If
Next

this code will skip slides. Use reverse iteration here as follows:

Dim I As Long
For I = oMasterPres.Slides.Count To 1 Step -1
Set oMasterSld = oMasterPres.Slides(I)
If InStr(oMasterSld.Tags("IncludeIn"), "A") > 0 Then
Else
oMasterSld.Delete
End If
Next

that should solve your "deleting all of the correct ones except about 6
slides" problem.

- Chirag

PowerShow - View multiple PowerPoint slide shows simultaneously
http://officeone.mvps.org/powershow/powershow.html
 
G

Guest

Thank you, that did the trick, but it leaves me wondering why...how come mine
skips code...and in the middle? The working code; the Count to line, that
counts all of the slides, then works backward?
 
C

Chirag

When you use For Each loop for all slides, it iterates through each slide.
At each iteration, the code till "Next" is executed with a slide as a
reference. If you delete the current slide, the next slide becomes the new
reference slide (internally). When you enter the next iteration, the
reference slide changes to a slide next to the current reference slide in
the deck. This means you skipped a slide that had internally become the new
reference slide during the previous iteration. So whenever you delete a
slide, the next slide will be skipped. For instance, if you iterate through
all slides and delete every slide you come across as follows:

For Each oMasterSld In oMasterPres.Slides
oMasterSld.Delete
Next

you will end up deleting only alternate slides - that is half the number of
slides.

Does this make sense?

- Chirag

PowerShow - View multiple PowerPoint slide shows simultaneously
http://officeone.mvps.org/powershow/powershow.html
 
D

David M. Marcovitz

When deleting things, you always have to step backward. That is because
PowerPoint renumbers things as they are deleted, so if you try to delete
later-numbered things, they are the wrong things. Additionally, your loop
skips the thing after the thing that is deleted. Also, note that For Each
is just shorthand for iterating through each object in order as if you
used a For i = 1 to ...

Let's say that you have objects 1 through 10, and you want to delete
objects 3 and 7.

Cycle 1 looks at object 1 and doesn't delete it.
Cycle 2 looks at object 2 and doesn't delete it.
Cycle 3 looks at object 3 and deletes it causing everything to be
renumberd:

Instead of 1 2 4 5 6 7 8 9 10, you have 1 2 3 4 5 6 7 8 9 where 3 4 5 6 7
8 9 are what used to be 4 5 6 7 8 9 10.

Cycle 4 looks at the newly numbered object 4, which used to be object 5,
having skipped the newly numbered object 3, which used to be object 4. If
you were basing deletions on some condition, then you never would have
looked at the newly numbered object 3, which was originally object 4.

Cycling backward prevents this because any renumbering happens to objects
you have already looked at. That is, when you delete object 7 (in my
example), objects 8, 9, and 10 will be renumbered, but that is OK becuase
you're done with them, and you will continue on to the untouched object
6, which still has its original number.

--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

Thank you! It makes sense, now! It was driving me crazy not being able to
figure out what the pattern was.
 

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

Similar Threads


Top