Using VBA to navigate slides in a presentation?


G

Guest

As a newbie I assume this is a trivial question but I can't get the syntax
right.

I have a number of slides with several drawing objects on each.

I have a simple menu as part of the presentation screen (as a master slide)
to launch macros that among other things, each needs to show a specific slide
(I have named each slide) and the objects on them are selectively made
visible/not visible.

All I am seeking is a line or two of VBA that will enable random access to
slides in my slide show.

I show/hide shapes using syntax like this:

ActivePresentation.Slides(1).Shapes("MyShape").Visible = msoTrue
ActivePresentation.Slides(1).Shapes("MyShape").Visible = msoFalse

but have not suceeded in selecting the required slide ( and showing it) yet.

Any tips would be appreciated

Thanks

Bob L
 
Ad

Advertisements

D

David M. Marcovitz

When you say "random," do you really mean random, or do you mean that you
want to be able to click on a button to take you to a specific slide? If
you just want to go to a specific slide, then you can do something like:

ActivePresentation.SlideShowWindow.View.GotoSlide 5

That takes you to slide 5 (change the 5 to whatever you want). If you
truly mean random (or pseudo-random since I said "truly" and there might
be some mathematicians listening), then you are talking about something a
bit more complicated. You might look at Exampels 8.16 and 8.17 on my site
(http://www.PowerfulPowerPoint.com/) to get one idea of how to move to a
random slide.

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

David,

I meant radom in the RAM sense i.e. user has free choice rather than
sequential.

I will try your code but I think I had problems with that. I also wish to
refer to the slides by the name I have given them.

Thanks
 
G

Guest

David,

I have now checked slide access using the suggested code and that works fine
using the numerical position of the slide as the value in the code e.g. 5 for
the fifth slide. However I wanted to goto my named slide e.g. "MyNamedslide"

Thank you
 
S

Steve Rindsberg

I have now checked slide access using the suggested code and that works fine
using the numerical position of the slide as the value in the code e.g. 5 for
the fifth slide. However I wanted to goto my named slide e.g. "MyNamedslide"

You can do that as well. This function returns the slide index of any named
slide (or 0 if there's no slide by that name):

Function IndexOfSlideNamed(sSlideName As String) As Long
' Returns the slide index of the named slide or 0 if slide not present

Dim oSl As Slide
Dim lTemp As Long

For Each oSl In ActivePresentation.Slides
If UCase(oSl.Name) = UCase(sSlideName) Then
lTemp = oSl.SlideIndex
End If
Next

IndexOfSlideNamed = lTemp

End Function

To test it:

Sub TestMe()
MsgBox IndexOfSlideNamed("Bubba")
End Sub


To use it, you'd do something like:

Dim lTemp as Long
lTemp = IndexOfSlideNamed("Bubba")
If lTemp > 0 Then ' you know the slide exists
ActivePresentation.SlideShowWindow.View.GotoSlide lTemp
Else
' do whatever you need to do if the slide's not there
End If
 
D

David M. Marcovitz

Steve's code is the complete version with all the bells and whistles
(mainly error checking). I usually use something like:

ActivePresentation.SlideShowWindow.View.GotoSlide _
ActivePresentation.Slides("MyNamedSlide").SlideIndex

It's simple, but if you screw up (like trying to go to a named slide that
doesn't exist), it won't do anything.

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

Advertisements

S

Steve Rindsberg

David M. said:
Steve's code is the complete version with all the bells and whistles
(mainly error checking). I usually use something like:

ActivePresentation.SlideShowWindow.View.GotoSlide _
ActivePresentation.Slides("MyNamedSlide").SlideIndex

It's simple, but if you screw up (like trying to go to a named slide that
doesn't exist), it won't do anything.

And as long as you test the presentation beforehand to make sure it all works,
I'd go with yours, especially if there are lots of slides in the show. It'll
be marginally faster, I'd bet.
 
Ad

Advertisements

G

Guest

David,

This is even better for my purpose as I keep a list of all slide and object
names and have a macro to read/write/edit them if needed.
 

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