dennis96411
dennis96411

Reputation: 127

Shortening GoToSlide Command in VBA?

I'm making a PowerPoint where I'm constantly using this command to jump around slides:

With SlideShowWindows(1)
    .View.GoToSlide (.Presentation.Slides(x).SlideIndex)
End With

I wanted to shorten that by writing a module that would shorten it, but since I'm a newbie and don't really know how, I really need help. This is the "SlideControl" module I've written:

Public Intro As String, BIOS As String, OSBegin As String, InitialSetup As String, LogIn As String, Desktop As String

Public Sub GoToSlide(Slide)

Intro = SlideShowWindows(1).Presentation.Slides(1).SlideIndex
BIOS = SlideShowWindows(1).Presentation.Slides(2).SlideIndex
OSBegin = SlideShowWindows(1).Presentation.Slides(5).SlideIndex
InitialSetup = SlideShowWindows(1).Presentation.Slides(6).SlideIndex
LogIn = SlideShowWindows(1).Presentation.Slides(9).SlideIndex
Desktop = SlideShowWindows(1).Presentation.Slides(11).SlideIndex

SlideShowWindows(1).View.GoToSlide (Slide)

End Sub

That is the entire module, nothing else at the end. Of course I'm new to coding, so if the code looks wrong please help me correct it. I think I should be able to go to the intro slide using this command:

GoToSlide (Intro)

Then I get this error when I call it:

Compile error:

Only comments may appear after End Sub, End Function, or End Property

Can anyone help me fix this? I'd appreciate it very much.

Upvotes: 2

Views: 3528

Answers (1)

Daniel
Daniel

Reputation: 13142

You're really close.

Based on what you've shown, you just need to make sure you list this:

Intro = SlideShowWindows(1).Presentation.Slides(1).SlideIndex
BIOS = SlideShowWindows(1).Presentation.Slides(2).SlideIndex
OSBegin = SlideShowWindows(1).Presentation.Slides(5).SlideIndex
InitialSetup = SlideShowWindows(1).Presentation.Slides(6).SlideIndex
LogIn = SlideShowWindows(1).Presentation.Slides(9).SlideIndex
Desktop = SlideShowWindows(1).Presentation.Slides(11).SlideIndex

In a procedure that runs before you need to call the values.

Alternately, since you're basically hardcoding the values, I think you could reasonably accomplish this with an enumeration. Note the enumeration has to go at the top of the module before any of the methods.

Public Enum slideNum
    Intro = 1
    Bios = 2
    OSBegin = 5
    InitialSetup = 6
    Login = 9
    Desktop = 11
End Enum
Public Sub GoToSlide(slide As slideNum)
    SlideShowWindows(1).View.GoToSlide (slide)
End Sub
Sub example()
    GoToslide(Login)
End sub

Using this second method when you are coding the GoTo Slide sub into a procedure it will automatically suggest valid slidenums I.E. Intro, Bios,OSBegin, InitialSetup,Login, or Desktop.

Upvotes: 1

Related Questions