Automatically adding PowerPoint slide numbers

Introduction

I was creating a big slide deck with slides for a detailed TM1 / PA administrator training for one of our major customers. I wanted to add a slide number, but more than that. I wanted to have something like the below:

This is the bottom-right corner of my slide. It reads the slide number as:

  • slide number 147
  • out of 193 slides in total
  • chapter 7
  • out of 10 chapters
  • within the 7th chapter, this is slide number 22
  • out of 42 slides in total for the chapter

Each chapter corresponds to a section within the PowerPoint presentation. I find sections a convenient way to split up a large presentation in smaller parts. For a large number of slides, I find it a good way of showing "where we are".

The question is: how can we add it to PowerPoint ? I found the slide number, that was an easy one, but the other counters… ? I figured I needed to write some VBA code for that. As I find it a good piece of automation, here it is and grab it ! :-)

Sub Extended_Slide_Number_In_Footer()
' Wim Gielis ' https://www.wimgielis.com
''''' ' Code to add slide numbers in PowerPoint ' 28/10/20 '''''
Dim sld As Slide Dim shp As Shape Dim s As String With ActivePresentation If .SectionProperties.Count > 0 Then For Each sld In .Slides sld.HeadersFooters.Footer.Visible = True i = sld.sectionIndex 'loop to look for the shape for the page number For Each shp In sld.Shapes If shp.Type = msoPlaceholder Then If shp.PlaceholderFormat.Type = 15 Then 'ppPlaceholderFooter = 15 If GetFirstSlideNumber(.SectionProperties.Name(sld.sectionIndex)) = sld.SlideIndex Then s = "" Else s = "p. [p]/[p_cnt] (ch. [ch]/[ch_cnt]: [p_ch]/[p_ch_cnt])" s = Replace(s, "[p]", sld.SlideNumber) s = Replace(s, "[p_cnt]", .Slides.Count) s = Replace(s, "[ch]", i) s = Replace(s, "[ch_cnt]", .SectionProperties.Count) s = Replace(s, "[p_ch]", sld.SlideIndex - GetFirstSlideNumber(.SectionProperties.Name(i)) + 1) s = Replace(s, "[p_ch_cnt]", .SectionProperties.SlidesCount(i)) End If shp.TextFrame.TextRange = s End If End If Next Next End If End With MsgBox "Done !"
End Sub
Public Function GetFirstSlideNumber(ByVal sectionName As String) As Long
' Returns the index of the first slide under the section "sectionName" ' Returns -1 if the section is not found or doesn't have any slides ' https://stackoverflow.com/questions/58529919/how-to-get-slide-number-of-first-slide-in-a-section
With ActivePresentation.SectionProperties Dim i As Long For i = 1 To .Count If .Name(i) = sectionName Then GetFirstSlideNumber = .FirstSlide(i) Exit Function End If Next End With ' Section not found GetFirstSlideNumber = -1
End Function

An additional complexity I added, is that the first slide of each section is a title slide. That slide does not get a slide number because it would overlap a nice picture: aesthetically, it is not 100% appealing.

It would now be easy to add the section name (chapter name) to the slide as well - should you want it. Maybe it exists out of the box in PowerPoint, I am not fully sure, but at least in code it's not a big deal.

Am I missing something ? Shouldn't this be easier to do ? Anyway, the work is done, serve yourself !

Keep on Excelling (PowerPoint is just an addon to Excel) !




Homepage

Section contents

About Wim

Wim Gielis is a Business Intelligence consultant and Excel expert

Other links