Automatic slide numbering in PowerPoint using VBA - vba

I have added a label in PowerPoint 2013 with the following code to show "x of y" at the bottom of all slides, where x is current slide number and y is total number of slides:
Private Sub Label1_Click()
Dim p1 As String
p1 = " of "
Dim p2 As Integer
Dim slideNumber As String
slideNumber = ActiveWindow.Selection.SlideRange.slideNumber
p2 = ActivePresentation.Slides.Count
Label1.Caption = slideNumber & p1 & p2
End Sub
The code works perfectly for the slide on which I have added the label e.g. it shows "9 of 29" for slide 9 of my total 29 slides, however when I copy and paste the label on other slides, it still shows "9 of 29" which is wrong as I expect it to automatically reflect the current slide number.

If you want this to work across all slides, wouldn't it be simpler to have a single button whose click updates all of the slide numbers at one time?
Assuming you have a shape named "SlideNumber" on every slide:
Sub ForExample()
Dim oSl As Slide
For Each oSl In ActivePresentation.Slides
With oSl.Shapes("SlideNumber").TextFrame.TextRange
.Text = "Slide " & CStr(oSl.SlideIndex) & " of " _
& CStr(ActivePresentation.Slides.Count)
End With
Next
End Sub

Here is my solution. Add a new textbox if it does not exist; otherwise use the existing one. That way, you can rerun the macro after making changes to slides.
Sub SlideNum()
For Each oSl In ActivePresentation.Slides
exist = False
For Each oS in Osl.Shapes
If oS.name = "SlideNum" Then
exist = True
Exit For
End If
Next
If exist = False Then
' convert from inch *72
Set oshp = oSl.Shapes.AddTextbox(msoTextOrientationHorizontal, 25.55*72, 14.21*72, 1.12*72, 20)
oshp.name = "SlideNum"
Else
Set oshp = oS
End If
With oshp.TextFrame.TextRange
' .Text = CStr(oSl.SlideIndex) & "/" & CStr(ActivePresentation.Slides.Count)
.Text = CStr(oSl.SlideIndex)
.Font.Size = 40
End With
Next
End Sub

Related

Listing the selected slide index numbers

I have a large presentation and I'm looking to select a number of slides positioned a different positions and receive a list of the slide index numbers. My idea is for this list to appear in a message box.
So far I have put together the below which gives me each slide number in different message boxes. I'm looking for a list to appear in 1 message box that I can copy and paste into an email.
Ideally I'd like to add some extra text to the message box so I can copy and paste to a pre made email. I can probably take that part on though.
Any help would be greatly received.
Sub ShowMeSlideNumbers()
Dim oSld As Slide
For Each oSld In ActiveWindow.Selection.SlideRange
MsgBox "The slide index of the current slide is:" & oSld.SlideIndex
Next oSld
Exit Sub
End Sub
You can do this by creating an array to store the selected index numbers. Then read the array numbers back in reverse order to create a string. Finally, display that string as part of a MsgBox:
Sub ShowMeSlideNumbers()
Dim oSld As Slide
Dim X As Integer, Y As Integer, arrSlideNumbers() As Integer
Dim strSlideNumbers As String
X = 0
For Each oSld In ActiveWindow.Selection.SlideRange
ReDim Preserve arrSlideNumbers(X)
arrSlideNumbers(X) = oSld.SlideIndex
X = X + 1
Next oSld
For Y = UBound(arrSlideNumbers) To 2 Step -1
strSlideNumbers = strSlideNumbers & CStr(arrSlideNumbers(Y)) & ", "
Next Y
strSlideNumbers = strSlideNumbers & CStr(arrSlideNumbers(1)) & " and "
strSlideNumbers = strSlideNumbers & CStr(arrSlideNumbers(0))
MsgBox "The slide indices of the selected slides are: " & strSlideNumbers & "."
End Sub
A simple mod of your existing code should do it:
Sub ShowMeSlideNumbers()
Dim oSld As Slide
Dim sTemp as string
For Each oSld In ActiveWindow.Selection.SlideRange
sTemp = sTemp & "The slide index of the current slide is:" & oSld.SlideIndex & VbCRLF
Next oSld
MsgBox sTemp
End Sub

Export Only Slides Within a Certain Section in Powerpoint VBA

I have code that exports slides to PNG files if they meet certain criteria (i.e. have a certain named shape in the slide). There will be occassions where slides will not have any known shape names, but they will be within a named "section".
I know I must somehow use the ActivePresentation.SectionProperties, but I am not sure how to go about doing this. I've tried things along the line of the below code with no success. In this example the name of the section is "Test". There will be many different sections and I would need to do this for several of those sections. Any help would be much appreciated. Thank you!
Dim sld As Slide
i = 1
For Each sld in ActivePresentation.Slides
If ActivePresentation.SectionProperties.Name("Test") Then
ActivePresentation.Slides(i).Export filenamepng & "TEST" & i & ".png", "PNG"
End If
i = i + 1
Next
#Hunter21188
I guess this is what you need.
You will check of which section every slide belongs.
After this you verify if it's from "Test" section, if is true gotcha! Export.
Obs. The function convert SectionIndex, from Slide Atribute to SectionName, that is not in Slides collection.
Sub Test_Export()
Dim sld As Slide
i = 1
DesiredSection = SectionIndexOf("Test")
For Each sld In ActivePresentation.Slides
If sld.sectionIndex = DesiredSection Then
ActivePresentation.Slides(i).Export filenamepng & "TEST" & i & ".png", "PNG"
End If
i = i + 1
Next
End Sub
Function SectionIndexOf(sSectionName As String) As Long
Dim x As Long
With ActivePresentation.SectionProperties
For x = 1 To .Count
If .Name(x) = sSectionName Then
SectionIndexOf = x
End If
Next
End With
End Function

VBA Loop Within a Loop Where the Second Loop Always Starts at 1

I have the following code below that loops through all slides in a PPT presentation. If the slides fall under one of the sections and are not hidden, it will save them with an appropriate name. I am trying to get the number after the saved name (i.e. TEST#) to always start over at 1 for each section. So if there are five slides within the IDSS section, which starts at slide 5, then they would be saved: IDSS1, IDSS2, etc. The way I have it below just saves it with the current slide number. Thanks for any help you can provide!
Edit to Provide More Clearity
I have many different slides in this PPT presentation, all of which are located within a particular named section. For this example, assume there are 6 total slides. Slides 1-3 are in the section named "TEST" and slide 3 is hidden. Slides 4-6 are in the section "IDSS" and none are hidden. I need the code to loop through all the slides, figure out which ones are in the "TEST" section and name them "TEST1" and "TEST2" (the third slide is skipped since it is hidden). Then it moves on to the section "IDSS" and saves the slides as "IDSS1", IDSS2", and "IDSS3". It's the number after the name I can't get to restart at 1 for each section. Hope this makes it more clear.
Dim sld As Slide
TestSection = SectionIndexOf("Test") 'Name of a section is in the quotes.
IDSSslides = SectionIndexOf("IDSS")
i = 1
For Each sld In ActivePresentation.Slides
If sld.sectionIndex = TestSection And ActivePresentation.Slides(i).SlideShowTransition.Hidden = msoFalse Then
ActivePresentation.Slides(i).Export filenamepng & "TEST" & i & ".png", "PNG"
ElseIf sld.sectionIndex = IDSSslides And ActivePresentation.Slides(i).SlideShowTransition.Hidden = msoFalse Then
ActivePresentation.Slides(i).Export filenamepng & "IDSS" & i & ".png", "PNG"
End If
i = i + 1
Next
Your question is not really clear, but I think that this is what you need, try it and let me know
Dim sld As Slide
TestSection = SectionIndexOf("Test") 'Name of a section is in the quotes.
IDSSslides = SectionIndexOf("IDSS")
dim i as integer 'Counter for TEST
dim j as integer'Counter for IDSS
i = 1
j=1
For Each sld In ActivePresentation.Slides
If sld.sectionIndex = TestSection And sld.SlideShowTransition.Hidden = msoFalse Then
sld.Export filenamepng & "TEST" & i & ".png", "PNG"
i=i+1
ElseIf sld.sectionIndex = IDSSslides And sld.SlideShowTransition.Hidden = msoFalse Then
sld.Export filenamepng & "IDSS" & j & ".png", "PNG"
j=j+1
End If
Next

TextBox Added to Each Slide Actually Added Many to Each Slide

I am very new at vba and need some guidance please. I am trying to add a text box to each slide (outside of the slide area), to view a slide's ID and Index at a quick glance. I have pieced together several bits to create the code below. it works, however the number of text boxes added to each slide is the number of slides in the presentation (112) instead of 1. I would also like to know how I can adapt this to be refreshed when changes to the slides are made.
Thanks in advance!
Here is the code:
Sub AddSlideInfo()
'Original Source: http://www.pptfaq.com/FAQ01180-Add-presentation-file-name-to-each-slide-master.htm
Dim x As Long
Dim oSh As Shape
Dim oSl As Slide
With ActivePresentation
On Error Resume Next 'In case the shape does not exist.
' On each slide in the presentation:
For x = 1 To .Slides.Count
Set oSl = ActivePresentation.Slides(x)
' Create a textbox at 0" from left,
' -120.24 points from top of slide ( -1.67") from top left corner
' Make it 90 points high, 300 points wide 1.25" x 5.5"
' Change any of these numbers at will
For Each oSl In ActivePresentation.Slides
With oSl
Set oSh = .Shapes.AddTextbox(Orientation:=msoTextOrientationHorizontal, Left:=0, Top:=-120, Width:=300, Height:=90)
' Give it a name so we can find it later
oSh.Name = "SlideInfo"
'Add some formatting and dummy text
With oSh.TextFrame.TextRange
.Font.Name = "Berlin Sans Demi"
.Font.Size = 12
.Text = _
"Slide Info: " & vbNewLine & _
"Slide Index: " & oSl.SlideIndex & vbNewLine & _
"Slide ID: " & oSl.SlideID
End With
End With
Next
Next x
End With
End Sub
Your code loops though all the slides with the For x = 1 To .Slides.Count and then loops through all slides again with For Each oSl In ActivePresentation.Slides. You don't need both.
The following is a simplified version of your code. It only loops through the slides once. It deletes the SlideInfo textbox if it exists (using On Error Resume Next to catch the error) ... but you can clean that up later :) ... and then recreates the textbox cleanly each time.
Option Explicit
Sub AddSlideInfo()
Const cShapeName = "SlideInfo"
Dim oSh As Shape
Dim oSl As Slide
On Error Resume Next
With ActivePresentation
For Each oSl In ActivePresentation.Slides
With oSl
.Shapes(cShapeName).Delete
Set oSh = .Shapes.AddTextbox(Orientation:=msoTextOrientationHorizontal, Left:=0, Top:=-120, Width:=300, Height:=90)
oSh.Name = cShapeName
'Add some formatting and dummy text
With oSh.TextFrame.TextRange
.Font.Name = "Berlin Sans Demi"
.Font.Size = 12
.Text = _
"Slide Info: " & vbNewLine & _
"Slide Index: " & oSl.SlideIndex & vbNewLine & _
"Slide ID: " & oSl.SlideID
End With
End With
Next
End With
End Sub

Add PowerPoint slide to textbox with CommandButton VBA

I have a command button and a text-box in the Slide Master of a PowerPoint presentation. I am trying to retrieve the PowerPoint's properties such as SlideID, SlideIndex and the name of the corresponding file and post them to the text box on the click of the command button.
At the moment I have this code but its giving me an error:
Sub CommandButton1_Click()
Dim Index As Long
Dim SlideId as Long
Dim FileName as String
TextBox1.Text = "SlideIndex:" & Index & "Slide ID:" & SlideId
End Sub
I want page 1 of the power point to read as slideIndex 1 SlideID 1 and the file name. and for slide 2 I want it to say all two's and so on...
Thanks in advance!
You can use a command button if you like; or you can use any PowerPoint shape you want to draw, assign it an Action Setting of Run Macro and choose the macro you want it to run when clicked.
Either way, this should work:
Sub ReportStuff()
Dim oSl As Slide
Dim oSh As Shape
Set oSl = SlideShowWindows(1).View.Slide
' Test to see if the shape's already there:
Set oSh = IsItThere(oSl, "My Text Box")
' If it's not there, add it:
If oSh is Nothing Then
Set oSh = oSl.Shapes.AddTextbox(msoTextOrientationHorizontal, 100, 100, 200, 50)
oSh.Name = "My Text Box"
End If
With oSh.TextFrame.TextRange
.Text = "Index: " & oSl.SlideIndex & " ID: " & oSl.SlideID & " File: " & ActivePresentation.FullName
End With
End Sub
Function IsItThere(oSl as Slide, sName as String) as Shape
Dim oSh as Shape
For each oSh in oSl.Shapes
If oSh.Name = sName Then
Set IsItThere = oSh
Exit Function
End If
Next
End Function