#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)
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 itin the comments section
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)
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
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.
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.
Software developer and coffee enthusiast. Raised in Alaska, working in Dallas.
I graduated from Southern Methodist University in 2016 with a B.S. in Computer Science and a B.A. in Psychology. I work for a pretty cool consulting company that hired me out of college to be a part of their Integration and Data Services team (aka I'm a Software Developer), and I couldn't imagine being anywhere else.
Since the stress of work pales compared to the stress of taking 19-24 hours of classes a semester, I've taken it upon myself to create mini-projects for myself that involve data mining, photography, and social gamification (and cute animals). If you're interested in any of those things you can read about the strategies here, or you can follow me on Instagram and watch the experiments in action (@jonesdoeslife or @zoraflorasays).