View Single Post
  #4  
Old March 13th, 2007, 10:57 AM posted to microsoft.public.powerpoint
Bill Dilworth
external usenet poster
 
Posts: 1,455
Default Why need to run macro twice?

In addition to John's point on Dimming as Boolean, whenever deleting shapes,
slides, or objects it is a very good idea to work backwards.

Instead of using For Each statements, try using For X = Item.Count to 1
Step -1 style statements. The reason is clear when you stand back and look
at the problem.

Here I have two code examples to delete the 2nd slide.

-----
Example 1:
Sub WrongWay()

Dim oSld As Slide

For Each oSld In ActivePresentation.Slides
If oSld.SlideIndex = 2 Then oSld.Delete
Next

End Sub
-----
This method will delete all the slides from 2 thru the end of the
presentation because as slide 2 gets deleted, the next slide (3) becomes
slide 2 and gets deleted also. This isn't intuitive on first glance and can
be a bit disastrous.

-----
Example 2:
Sub RightWay()

Dim oSld As Slide, x As Integer

With ActivePresentation
For x = .Slides.Count To 1 Step -1
Set oSld = .Slides(x)
If oSld.SlideIndex = 2 Then oSld.Delete
Next x
End With

End Sub
-----

This time, the deletion of slide 2 does not shift the slides you are looking
at next, only the ones you have already checked. It is a little more code,
but not that complicated.

The same principles apply to shapes within a slide or within any collection
of objects.


Soooooo.... after that long introduction, I would suspect that the deletion
of the first placeholder, shifted the objects so that the next one was not
found, therefore the double run requirement. Of course this is just my
quick guess.



--
Bill Dilworth
A proud member of the Microsoft PPT MVP Team
Users helping fellow users.
http://billdilworth.mvps.org
-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
vestprog2@ Please read the PowerPoint FAQ pages.
yahoo. They answer most of our questions.
com www.pptfaq.com
..


"John Wilson" john AT technologytrish.co DOT uk wrote in message
...
One quick thing Lisa

Dim myCheckBody, myCheckSlide As Boolean - doesn't do what I guess you
imagine it does. myCheckbody would be declared as a variant and only
myCheckslide as Boolean. It's a good idea to keep the Dim statements on
their
own line to avoid this easy slip.

Dim myCheckBody As Boolean
Dim myCheckSlide As Boolean
--
Amazing PPT Hints, Tips and Tutorials-http://www.PPTAlchemy.co.uk
http://www.technologytrish.co.uk/ppttipshome.html
email john AT technologytrish.co.uk


"Lisa" wrote:

I wrote a macro to delete the slide (title) placeholder from the
currently
viewed Notes page, then change the position of the body placeholder on
that
page. With my first attempt at the code, I needed to run the macro twice.
The
first run deleted the slide placeholder; the second run repositioned the
body
placeholder.

I rewrote the code so I need to run the macro only once, but I'm still
stumped as to why I needed to run the old version twice. (I'm still
pretty
much in the dark about VBA in general and VBA for ppt in particular.)

I'd really appreciate it if you could shed some light on this. (And if
you
see anything silly in the code below, please feel free to call it to my
attention, even if it has nothing to do with the question at hand.)

Thanks much!

HERE'S THE CODE IN QUESTION
Dim myShape As Shape
Dim mySlide As Slide
Dim myCheckBody, myCheckSlide As Boolean
Dim nn As Integer

Sub Fmt_Notes_FullTxt_OLD()
'Deletes the slide placeholder and puts body placeholder at top of
current
Notes page
'Run while appropriate Notes pg is active window
'Must run twice to get BodyPlaceholder positioned properly
Call Name_Placehldrs_CurSlide
On Error Resume Next
For Each myShape In ActiveWindow.Selection.SlideRange.Shapes
If myShape.Name = "SlidePlaceholder" Then
myShape.Delete
ElseIf myShape.Name = "BodyPlaceholder" Then
With myShape
.Left = 40
.Top = 50
End With
Else ' leave it alone
End If
Next
End Sub

Sub Fmt_Notes_FullTxt_NEW()
'Deletes the slide placeholder and puts body placeholder at top of
current
Notes pg
'Run while appropriate Notes pg is active window
'Need to run once only
Call Name_Placehldrs_CurSlide
On Error Resume Next
For Each myShape In ActiveWindow.Selection.SlideRange.Shapes
If myShape.Name = "SlidePlaceholder" Then myShape.Delete
Next
For Each myShape In ActiveWindow.Selection.SlideRange.Shapes
If myShape.Name = "BodyPlaceholder" Then
With myShape
.Left = 40
.Top = 50
End With
End If
Next
End Sub

Sub Name_Placehldrs_CurSlide()
'Names the body and slide (title) placeholders on each Notes pg
'Run from any view
myCheckBody = False
myCheckSlide = False
On Error Resume Next

nn = ActiveWindow.Selection.SlideRange.SlideIndex
Set mySlide = ActivePresentation.Slides(nn)
With ActivePresentation.Slides(nn).NotesPage.Shapes
For Each myShape In mySlide.NotesPage.Shapes
If myShape.PlaceholderFormat.Type = ppPlaceholderBody Then
myShape.Name = "BodyPlaceholder"
myCheckBody = True
ElseIf myShape.PlaceholderFormat.Type = ppPlaceholderTitle
Then
myShape.Name = "SlidePlaceholder"
myCheckSlide = True
Else 'leave it alone
End If
Next myShape
If myCheckBody = False Then
.AddPlaceholder(ppPlaceholderBody).Name = "BodyPlaceholder"
End If
If myCheckSlide = False Then
.AddPlaceholder(ppPlaceholderTitle).Name = "SlidePlaceholder"
End If
End With
End Sub