#Developer: Autogenerating (and Formatting) Table of Contents in Powerpoint using Macros

#Developer: Autogenerating (and Formatting) Table of Contents in Powerpoint using Macros

You can take the consultant out of a development role, but you can’t take the developer out of the tech consultant

So when my work-partner said “I added a slide at the beginning of this deck, can you update the seven table of contents slides embedded in this 100+ page deck to make sure all the slide numbers are still right?” I knew there had to be a better way to update them than manually changing all those slide numbers every time we wanted to add a new slide.

And there absolutely is.

The short of it is this: hyperlink your table of content numbers, set up a macro to automatically edit those hyperlinks, run that macro and enjoy your sweet, sweet, freedom

Step 1: Set Your Table of Contents Up for Success

Whether you have one slide with a table of contents on it, or 10 slides, this will work for you.

The only thing this specifically requires are titles (not hyperlinked) and some kind of text that will turn into a page number (this one needs to be hyperlinked)

Set up your basic table of contents
Hyperlink the items you want to become page numbers
Link them to the relevant pages in the document

Step 2: Set up a Macro

Macros are small routines you can use to add or edit your document in a programmatic way. This one is based off of a sweet subroutine I found on this blog, with the notable addition of accounting for multiple TOC slides and the color formatting change.

You can find the macros under the “View” tab or under Tool > Macro in the navigation bar at the top

This will open up a window where you can name the function and then press the “+” button
This opens up a Visual Basic window where you can write and edit the function

Step 3: Write (or Customize) the Macro

At a very high level, this is what we’re going to be doing in the macro:

Start the subroutine
    For every page that has a table of contents
        For every hyperlink on these pages
            Check to see what slide # that hyperlink is associated with
            Replace whatever the text is with that slide number
            Format the color of those links on that slide

The below is the actual code you’ll be putting into the macro:

You may want to change:

  • The slide numbers associated with Table of Contents
    • Though this is set up to handle multiple slides, it will also handle just one. You would put something like Array(2) instead of Array(2, 3, 4, 5)
  • The color you’re changing the links to
    • We use RGB color codes and changed it to black
    • Note: After a lot (and I mean a lot) of research, I haven’t been able to find a way to remove the line from underneath a hyperlink that doesn’t involve hyperlinking a transparent square and putting it on top of text. If you happen to find a way, I’d love to hear it in the comments section
Sub updateIndexes()
 Dim slideNumbs As Variant

' TODO: In Array() enter the slide number(s) for your table of contents (e.g. we have 7 slides for different TOC sections)
 slideNumbs = Array(6, 14, 22, 33, 51, 60, 69)
 
' Iterate through the table of contents slides
    For Each tocSlideNumber In slideNumbs
        Dim pTableOfContent As Slide
          Set pTableOfContent = ActivePresentation.Slides(tocSlideNumber)
 
' Iterate through the links on the slide 
          For Each pHyperlink In pTableOfContent.Hyperlinks
              Dim pLinkNumber As String
              Dim pLinkedSlide As Slide
              pLinkNumber = Left(pHyperlink.SubAddress, InStr(pHyperlink.SubAddress, ",") - 1)

' Set the text to whatever the slide number of the hyperlink is 
              pHyperlink.TextToDisplay = ActivePresentation.Slides.FindBySlideID(CLng(pLinkNumber)).SlideIndex
              
' Set the hyperlink color for this slide to black 
              pTableOfContent.ThemeColorScheme.Colors(11) = RGB(0, 0, 0)
          Next pHyperlink
    Next tocSlideNumber
End Sub

Step 4: Run the Macro

Aces, you’ve got the code, you’re well on your way to making this work. As soon as you’ve customized it to fit your needs you can hit the “Run” button at the top of the window

Voila, all your hyperlinks have been transformed

If you shift your slides around (obviously I’ll probably want to move “Important Point” before “Diagrams”) you’ll want to re-run the Macro to make sure the slide numbers are updated correctly.

TL;DR

Tired of manually updating slide numbers for your triple digit decks in PowerPoint? Fear not, there’s a programmatic solution. With hyperlinks and a PowerPoint VBA Macro, you can create a solution that lets you set up all your Table of Contents with the click of a button.



Share your experience

This site uses Akismet to reduce spam. Learn how your comment data is processed.