PPT2000: Sample VBA Macro to Start Page Numbers After First Slide (259823)
The information in this article applies to:
- Microsoft PowerPoint 2000
This article was previously published under Q259823 SUMMARY
If you want to skip the first few slides when you number the slides in a presentation - for example,
if you want slide 3 to have the number 1 - you typically have to create the text frame for each slide
number manually. This article contains a macro that automates most of this procedure.
MORE INFORMATIONMicrosoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. NOTE: The following macro examples work only in PowerPoint. Visual Basic for Applications macros are not supported by the Microsoft PowerPoint Viewer. For additional information, click the following article number to view the article in the Microsoft Knowledge Base: Sample Macro
The following sample macro allows you to pick a slide to start numbering from 1, or to redo slide numbers
that you created with this macro. The macro puts the custom property Page Numbers into the
custom document properties. If you want to change the first slide from which to start numbering, change
the value of this property.
Sub numberSlides()
' On Error, continue with running macro.
On Error Resume Next
Dim i As Long
Dim j As Long
Dim bProperty As Boolean
Dim lStart As Long
Dim oShape As Shape
Dim sShapeName As String
' Set bProperty flag to true. If there is no custom property
' called "Page Numbers", then create new text frames.
'
bProperty = True
' Loop through all the custom properties. If none, subroutine
' drops out of the loop. Check to see if the custom property
' is equal to "Page Numbers", if so, retrieve the value stored.
'
For j = 1 To ActivePresentation.CustomDocumentProperties.Count
If ActivePresentation.CustomDocumentProperties.Item(j) _
.Name = "Page Numbers" Then
lStart = ActivePresentation.CustomDocumentProperties.Item(j).Value
' Loop through the presentation, starting at the saved slide
' index position.
'
For i = lStart To ActivePresentation.Slides.Count
' Create the shape's name based on the current slide name.
' This is done because each page number shape created with
' this macro has a unique name and can be referenced simply
' by constructing the name using the same parameters. If you
' change the name of the slide, delete the page number frame
' to allow it to re-create it.
'
sShapeName = "Page Num " & ActivePresentation.Slides(i).Name
' Set oShape to be the name generated above.
'
Set oShape = ActivePresentation.Slides(i).Shapes(sShapeName)
' If an Error number other than 70 is returned, then
' create a new frame and place it on the slide. Otherwise
' update the existing frame with the current page number.
'
If Err.Number <> 70 Then
MakePageNum i, lStart, sShapeName
Err.Number = 70
Else
ChangeExist oShape, i, lStart
End If
Next i
' Set bProperty to false to prevent the subroutine from
' running the code after this section.
'
bProperty = False
End If
Next j
' If bProperty is true, then this is the first time this
' macro has been run for this presentation.
'
If bProperty Then
' Prompt user for starting slide from which to begin numbering.
'
lStart = CDbl(InputBox("What slide does page 1 to start on?", _
"Page Numbering", "1"))
' Check for valid lStart value. If incorrect, end the macro.
'
If lStart <= 0 Then
MsgBox "Value must be between 1 and " & _
ActivePresentation.Slides.Count
Exit Sub
End If
' Create the custom property "Page Numbers" and set it
' equal to the user-provided value.
'
ActivePresentation.CustomDocumentProperties _
.Add Name:="Page Numbers", LinkToContent:=False, _
Type:=msoPropertyTypeNumber, Value:=lStart
' From the starting slide to the end of the presentation,
' create a new text frame with the values for the page
' numbers.
'
For i = lStart To ActivePresentation.Slides.Count
sShapeName = "Page Num " & ActivePresentation.Slides(i).Name
MakePageNum i, lStart, sShapeName
Next i
End If
End Sub
Function ChangeExist(oShape As Shape, i As Long, lStart As Long)
' This function updates existing text frames in a
' presentation where the numberSlides macro has run
' before.
'
With oShape.TextFrame.TextRange
' Create slide number by adding one to the index value, and
' then substracting the starting index location from the
' result.
'
.Text = i + 1 - lStart
.Font.Name = "Times New Roman"
.Font.Size = 48
.ParagraphFormat.Alignment = ppAlignCenter
End With
End Function
Function MakePageNum(i As Long, lStart As Long, sShapeName As String)
' This function creates a new text frame and puts a page number into
' that text frame.
'
Dim oShape As Shape
' Create the new shape and place it in the lower-right
' corner of the slide. Slides are 720 points wide
' by 540 points tall. The upper-left corner is at position
' (0,0) the lower right corner is at (720,540).
'
Set oShape = ActivePresentation.Slides(i).Shapes _
.AddTextbox(msoTextOrientationHorizontal, 654, 408, 38.5, 64.875)
' Set the shape's name to the name passed to the function.
'
oShape.Name = sShapeName
With oShape.TextFrame.TextRange
' Create slide number by adding one to the index value, and
' then substracting the starting index location from the
' result.
'
.Text = i + 1 - lStart
.Font.Name = "Times New Roman"
.Font.Size = 48
.ParagraphFormat.Alignment = ppAlignCenter
End With
End Function
How to Change the Value of the Page Numbers Custom Property- With the presentation open, click Properties on the File menu.
- On the Custom tab, under Properties, click Page Numbers.
NOTE: This property is only available after you run the numberSlides macro above. - In the Value field, type the new slide number from which you want to start the slide numbering. Click OK.
NOTE: If you decide to skip previously numbered slides, you must manually delete the text frames from those slides.
Modification Type: | Minor | Last Reviewed: | 10/11/2006 |
---|
Keywords: | kbdtacode kbhowto KB259823 |
---|
|