Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Programmatically add a SlideNumber placeholder in PowerPoint 2010 using vba?

Posted on 2012-03-12
Medium Priority
Last Modified: 2012-03-13
Hi experts:

I'm writing a vba macro to clean up and standardize slide numbering in a PowerPoint (2010) deck assembled from other old decks.  Each slide might bring with it a different slide number placeholder, and they don't automatically conform.  My plan is to go to the master, delete any existing slidenumber placeholder, re-add it and format it like I want it.  Then go to each layout and delete and re-display the placeholder, then finally go to each slide and do the same.

I know how to access and can successfully manipulate the slide number placeholder if it already exists on any master, layout, or slide, but can't figure out how to ADD the slide number placeholder if it does not already exist.   The HeadersFooters.SlideNumber object has only a handful of properties (such as .visible true or false) that aren't very useful for what I'm trying to do.  I tried just adding a shape to the master named "Slide Number Placeholder 1" but PowerPoint does not recognize that as the HeadersFooters.SlideNumber object.

Essentially, I want vba to duplicate this series of steps (where's that dang macro recorder when you need it?!):
-(Go to master)
-Master Layout
-(Check Slide Number Placeholder box)

Or, for a layout it would be:
(go to desired layout)
(in the Master Layout group), check "Footers"

Or, for a slide it would be:
Slide Number
Check Slide Number

Appreciate any pointers!
Question by:Bryce Bassett
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2

Author Comment

by:Bryce Bassett
ID: 37711851
Did a little more experimenting.  

(Slide).HeadersFooters.SlideNumber.Visible = msoTrue

does display the page number on a slide, IF that slide's layout already has a SlideNumber placeholder.  But still need to figure out how to add that placeholder to masters and layouts.

LVL 23

Accepted Solution

JSRWilson earned 2000 total points
ID: 37711906
You should only ADD to Masters and Layouts. In slides make it visible.

For masters and Layouts you must check that the placeholder is NOT present or there will be an error (there can only be one)


Sub addPLH()
If Not ExistNum(ActivePresentation.Designs(1).SlideMaster) Then
ActivePresentation.Designs(1).SlideMaster.Shapes.AddPlaceholder _
Type:=ppPlaceholderSlideNumber, _
Left:=520, _
Top:=500, _
Width:=150, _
End If
End Sub

Function ExistNum(osld As Object) As Boolean
Dim oshp As Shape
ExistNum = False
For Each oshp In osld.Shapes
If oshp.Type = msoPlaceholder Then
If oshp.PlaceholderFormat.Type = ppPlaceholderSlideNumber Then
ExistNum = True
End If
End If
End Function

Open in new window

Code would be similar for layouts

Author Comment

by:Bryce Bassett
ID: 37712461
Thanks a ton!  .AddPlaceHolder is what I was missing.    I've got this working the way I want with one exception.

Notice below that after I add the slide number placeholder to the Master slide, I do some formatting for font, color, size, and I put the word "page " before the slide number.  When I add the slide placeholder to each layout, all of those formatting changes are replicated, except that the word page does not appear.  If I manually remove any layout's page number placeholder which VBA added and re-insert it by checking the "Footers" checkbox in the Master Layout group of the Slide Master tab, it adds the placeholder INCLUDING the word "page " as I would expect it to.  

Any ideas why it's not showing "page " and how to fix that?

-------------------------------------------  (can't figure out how to embed code)

Sub FixPageNumbers(control As IRibbonControl)

If Application.Presentations.count = 0 Then
    MsgBox "A presentation must be open before using this command."
    Exit Sub
End If

Dim mylayout As CustomLayout
Dim ckshp As Shape
Dim myslide As Slide
Dim x As Integer
Dim sm As Master
Dim newPH As Shape

'delete existing slide number placeholder
For x = 1 To ActivePresentation.Designs.count

    Set sm = ActivePresentation.Designs(x).SlideMaster
    For Each ckshp In sm.Shapes
        If ckshp.Type = msoPlaceholder Then
            If ckshp.PlaceholderFormat.Type = ppPlaceholderSlideNumber Then
            End If
        End If
    'insert formatted slide number placeholder on master
    Set newPH = sm.Shapes.AddPlaceholder(Type:=ppPlaceholderSlideNumber, _
                Left:=546, Top:=496, width:=168, height:=29)
    With newPH.TextFrame.TextRange
        .Font.Name = "Calibri"
        .Font.Size = 9
        .Font.Color = RGB(137, 137, 137)
        .ParagraphFormat.Alignment = ppAlignRight
        .text = "page "
    End With
    'delete existing slide number placeholder on layouts
    For Each mylayout In sm.CustomLayouts
        For Each ckshp In mylayout.Shapes
            If ckshp.Type = msoPlaceholder Then
                If ckshp.PlaceholderFormat.Type = ppPlaceholderSlideNumber Then
                End If
            End If
    Next mylayout

    'insert slide number placeholder on layouts
    For Each mylayout In sm.CustomLayouts
        Set newPH = mylayout.Shapes.AddPlaceholder(Type:=ppPlaceholderSlideNumber)
    Next mylayout

Next x

'toggle page # off and back on in every slide
For Each myslide In ActivePresentation.Slides
    myslide.HeadersFooters.SlideNumber.Visible = msoFalse
    myslide.HeadersFooters.SlideNumber.Visible = msoTrue

End Sub
LVL 23

Expert Comment

ID: 37714922
I don't think you need to insertslidenumber - isn't it there by default?

To get the text use InsertBefore

With newPH.TextFrame.TextRange
        .Font.Name = "Calibri"
        .Font.Size = 9
        .Font.Color = RGB(137, 137, 137)
        .ParagraphFormat.Alignment = ppAlignRight
        .InsertBefore ("Page ")
    End With

Open in new window


Author Closing Comment

by:Bryce Bassett
ID: 37716202
Thanks!  .InsertBefore did the trick.  

I went ahead and inserted the "Page " also on each layout as I add the placeholder.  I'm still curious to know why I have to do that on each layout, given that when you check the Footers box using the regular UI it inherits the placeholder (including "Page ") from the master slide.  If you have a solution for that, I'd be interested.

But I'm calling this solved.  Thanks again.

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Technology opened people to different means of presenting information, but PowerPoint remains to be above competition. Know why PPT still works today.
A lot of things can happen during a presentation, worst of which is “death by PowerPoint.” Here are a few mistakes to avoid to make your slides clean.
This video teaches viewers how to add simple and professional themes to their slides.
The viewer will learn how to edit animations within the presentation, incorporate sound, and set everything up with timing.

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question