Programmatically add a SlideNumber placeholder in PowerPoint 2010 using vba?

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?!):
-View
-SlideMaster
-(Go to master)
-Master Layout
-(Check Slide Number Placeholder box)
-OK

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

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

Appreciate any pointers!
Thanks,
Bryce BassettFreelance VBA programmerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bryce BassettFreelance VBA programmerAuthor Commented:
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.

Thx
JSRWilsonCommented:
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)

Example:

Sub addPLH()
If Not ExistNum(ActivePresentation.Designs(1).SlideMaster) Then
ActivePresentation.Designs(1).SlideMaster.Shapes.AddPlaceholder _
Type:=ppPlaceholderSlideNumber, _
Left:=520, _
Top:=500, _
Width:=150, _
Height:=30
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
Next
End Function

Open in new window


Code would be similar for layouts

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bryce BassettFreelance VBA programmerAuthor Commented:
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
                ckshp.Delete
            End If
        End If
    Next
   
    '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 "
        .InsertSlideNumber
    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
                    ckshp.Delete
                End If
            End If
        Next
    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
Next

End Sub
JSRWilsonCommented:
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

Bryce BassettFreelance VBA programmerAuthor Commented:
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft PowerPoint

From novice to tech pro — start learning today.