Search code examples
vbapowerpoint

Automatic slide numbering in PowerPoint using VBA


I have added a label in PowerPoint 2013 with the following code to show "x of y" at the bottom of all slides, where x is current slide number and y is total number of slides:

Private Sub Label1_Click()
Dim p1 As String
p1 = " of "
Dim p2 As Integer
Dim slideNumber As String
slideNumber = ActiveWindow.Selection.SlideRange.slideNumber
p2 = ActivePresentation.Slides.Count
Label1.Caption = slideNumber & p1 & p2

End Sub

The code works perfectly for the slide on which I have added the label e.g. it shows "9 of 29" for slide 9 of my total 29 slides, however when I copy and paste the label on other slides, it still shows "9 of 29" which is wrong as I expect it to automatically reflect the current slide number.


Solution

  • If you want this to work across all slides, wouldn't it be simpler to have a single button whose click updates all of the slide numbers at one time?

    Assuming you have a shape named "SlideNumber" on every slide:

    Sub ForExample()
        Dim oSl As Slide
    
        For Each oSl In ActivePresentation.Slides
            With oSl.Shapes("SlideNumber").TextFrame.TextRange
                .Text = "Slide " & CStr(oSl.SlideIndex) & " of " _
                    & CStr(ActivePresentation.Slides.Count)
            End With
        Next
    
    End Sub