Global variables are unavailable at startup

621 Views Asked by At

I have the following VBA code attached in a PowerPoint 2010 macro-enabled presentation:

Public CurrentSlideIndex As Integer

Sub OnSlideShowPageChange()
    CurrentSlideIndex = ActivePresentation.SlideShowWindow.View.CurrentShowPosition
    If CurrentSlideIndex = 1 Then
        MsgBox "First Page"
        ' some initialization
    End If
End Sub    

I want to do some initialization there when the first page is shown. The problem is that when I run the presentation for the first time, the routine is not fired. I need to stop the presentation and run it a second time, then it works, and keeps working afterward also. Only the first run doesn't work.

Is there a fix for that?

2

There are 2 best solutions below

0
On

I'd make a few changes to the code:

Public CurrentSlideIndex As Long

Sub OnSlideShowPageChange(ByVal SSW As SlideShowWindow)
    CurrentSlideIndex = SSW.View.CurrentShowPosition
    If CurrentSlideIndex = 1 Then
        MsgBox "First Page"
        ' some initialization
    End If
End Sub

Then (Thank you, Hans Hofman for this: http://www.tech-archive.net/Archive/Office/microsoft.public.powerpoint/2006-02/msg01234.html ) put a dummy active-x control on the first slide (or actually just OFF the first slide so it doesn't appear in the show).

That apparently forces VBA to initialize and voila, the event handler fires.

0
On

Pompair,

Try something along the lines of this:

Sub OnSlideShowPageChange(ByVal SlideSet As SlideShowWindow)
    If SlideSet.View.CurrentShowPosition = _
        SlideSet.Presentation.SlideShowSettings.StartingSlide Then
        MsgBox "I am a Message."
    End If
End Sub

Save the code in the module, the run the powerpoint from the beginning. I ran it in 2007, so I believe it will function fine in Powerpoint 2007 or 2010.

You can also use this code to write modules for other slide show positions. Check out the MSDN documentation for a more complete list of possibilities: 2007 -- http://msdn.microsoft.com/en-us/library/bb265987(v=office.12).aspx

2010 -- http://msdn.microsoft.com/en-us/library/ff746846.aspx

~JOL