Solved

Programmatically add a SlideNumber placeholder in PowerPoint 2010 using vba?

Posted on 2012-03-12
5
2,958 Views
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?!):
-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,
0
Comment
Question by:versatilebb
  • 3
  • 2
5 Comments
 

Author Comment

by:versatilebb
Comment Utility
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
0
 
LVL 23

Accepted Solution

by:
JSRWilson earned 500 total points
Comment Utility
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
0
 

Author Comment

by:versatilebb
Comment Utility
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
0
 
LVL 23

Expert Comment

by:JSRWilson
Comment Utility
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

0
 

Author Closing Comment

by:versatilebb
Comment Utility
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.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Introduction I recently received a question about the creation of Computer-Based Training (CBT) that incorporates quizzes in the middle of the lesson. The intent was to keep people engaged in the content, and not to actually track students’ scores.…
 Regular Expressions Microsoft Word has sophisticated search tools that can search for patterns. For example if you wanted to search for all UK phone numbers that followed a pattern of five digits, a space and then six digits you can easily do th…
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  create a slide that will launch other presentations in Microsoft PowerPoint. In the finished slide, each item launches a new PowerPoint presentation and when each is finished it automatically comes back to this slide: …

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now