Home > Software engineering >  How to automatically enable Slide number footers in Powerpoint using VBA code?
How to automatically enable Slide number footers in Powerpoint using VBA code?

Time:01-06

I already have a macro that creates a Powerpoint presentation from Excel, but I'm having trouble enabling the Slide Number footer options in the code.

Specifically, I want the equivalent VBA code for inserting the slide number by doing the following in Powerpoint. From the home ribbon, Go to Insert Tab > Click Header & Footer > Tick the checkbox for Slide number. This will then automatically add page numbers to all the slides so if the user changes anything like adding/removing/moving slides, the slide numbers will also subsequently change.

Note: Technically I could do a for loop through all the slides in Powerpoint and add textboxes on the lower left corner to add the slide numbers, but doing this, the intended user who is not tech-savvy would have to change the page number manually if they change anything in the Powerpoint because it will just be a textbox.

Here's what I figured out so far but it's not working.

Dim PPTApp As New PowerPoint.Application
Dim PPTPres As PowerPoint.Presentation
Dim ppt_template_path As String
ppt_template_path = "C:\Users\user\Documents\Performance Report Template.pptx"
PPTApp.Activate
Set PPTPres = PPTApp.Presentations.Open(ppt_template_path)

'---------this is where nothing happens when I try to enable Slide Number footers

If PPTPres.HasTitleMaster Then
    With PPTPres.TitleMaster.HeadersFooters
        .SlideNumber.Visible = msoTrue
    End With
End If

With PPTPres.SlideMaster.HeadersFooters
    .SlideNumber.Visible = msoTrue
End With

With PPTPres.Slides.Range.HeadersFooters
    .SlideNumber.Visible = msoTrue
End With

Btw I'm using Microsoft 365 version. So I want to know if you guys have any ideas please! I would really appreciate it!

CodePudding user response:

That's old code from the PowerPoint 2003 macro recorder (yes, PowerPoint used to have one!). Some VBA has changed since then. Please note the addition of Dim oSlide as Slide to the declarations:

Dim PPTApp As New PowerPoint.Application
Dim PPTPres As PowerPoint.Presentation
Dim ppt_template_path As String
Dim oSlide As Slide
ppt_template_path = "C:\Users\user\Documents\Performance Report Template.pptx"
PPTApp.Activate
Set PPTPres = PPTApp.Presentations.Open(ppt_template_path)

If PPTPres.HasTitleMaster Then
    PPTPres.TitleMaster.HeadersFooters.SlideNumber.Visible = msoTrue
End If

PPTPres.SlideMaster.HeadersFooters.SlideNumber.Visible = msoTrue

For each oSlide in PPTPres.Slides
    oSlide.HeadersFooters.SlideNumber.Visible = msoTrue
Next oSlide

Test that the existing presentation can turn on slide numbers using Insert>Header & Footer>Slide number>Apply to All. There are lots of templates out there where a designer has deleted the slide number placeholder from the master and layouts, making it impossible.

  • Related