Solved

Programmatically add a SlideNumber placeholder in PowerPoint 2010 using vba?

Posted on 2012-03-12
5
3,101 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
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.

Thx
0
 
LVL 23

Accepted Solution

by:
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)

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

0
 

Author Closing Comment

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

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Setting the Scene PowerPoint is a creative tool in the right hands but it also includes a much underutilised programming dimension. In this beginner level article, we're going to show you some of some key elements of programming PowerPoint using th…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
The viewer will learn how to edit the master slide. They will also learn how to combine multiple themes into one master slide to use them in their presentation.
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: …

776 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