Programmatically add a SlideNumber placeholder in PowerPoint 2010 using vba?

Posted on 2012-03-12
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:versatilebb
  • 3
  • 2

Author Comment

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 500 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

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

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction In all recent versions of PowerPoint it is possible to trigger animations. This means the animation takes place when a certain shape is clicked. This allows you to run animation “on demand” and outwith the normal sequence of mouse cl…
Technology opened people to different means of presenting information, but PowerPoint remains to be above competition. Know why PPT still works today.
This video teaches viewers how to create handouts from their slides and helps them decide how many slides to include per handout.
The viewer will learn how to edit animations within the presentation, incorporate sound, and set everything up with timing.

756 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