Sort a presentation based on slide titles
Problem
You want to sort a large presentation based on the slide titles.
Solution
Copy and paste the code below into a new module in a PowerPoint presentation (it needn't be the one you intend to sort; you just need to have this code available in an open presentation, then make the presentation you want to sort active). Shout out to Adam for finding and reporting problems with this routine.
Please use this ONLY on a copy of your presentation.
Option Explicit
Sub SortMe()
Dim x As Long
Dim rayTitles() As Variant
Dim lIndex As Long
ReDim Preserve rayTitles(1 To ActivePresentation.Slides.Count) As Variant
' collect titles and indices in array
' Sort array only works with 1-dimension arrays_
' so we'll combine the slide title and index
' then sort 'em out later:
For x = 1 To ActivePresentation.Slides.Count
rayTitles(x) = GetTitle(ActivePresentation.Slides(x)) & "|||" _
& CStr(ActivePresentation.Slides(x).SlideID)
Next
' sort the array
Call BubbleSortVariantArray(rayTitles())
' rearrange:
For x = 1 To UBound(rayTitles)
' split out the index of the slide
lIndex = CLng(Mid$(rayTitles(x), InStr(rayTitles(x), "|||") + 3))
Debug.Print rayTitles(x)
ActivePresentation.Slides.FindBySlideID(lIndex).MoveTo _
ActivePresentation.Slides.Count
Next
End Sub
Function GetTitle(oSld As Slide) As String
' return the slide title for oSld if any
Dim oSh As Shape
For Each oSh In oSld.Shapes
If oSh.Type = msoPlaceholder Then
If oSh.PlaceholderFormat.Type = ppPlaceholderTitle _
Or oSh.PlaceholderFormat.Type = ppPlaceholderCenterTitle Then
' it's a title
GetTitle = oSh.TextFrame.TextRange.Text
Exit Function
End If
End If
Next
End Function
Public Sub BubbleSortVariantArray(rayIn() As Variant)
Dim lLow As Long
Dim lHigh As Long
Dim intX As Long
Dim intY As Long
Dim varTmp As Variant
On Error GoTo Errorhandler
' Get the bounds of the array
lLow = LBound(rayIn)
lHigh = UBound(rayIn)
For intX = lLow To lHigh - 1
For intY = intX + 1 To lHigh
If rayIn(intX) > rayIn(intY) Then
varTmp = rayIn(intX)
rayIn(intX) = rayIn(intY)
rayIn(intY) = varTmp
End If
Next intY
Next intX
NormalExit:
Exit Sub
Errorhandler:
MsgBox "There was a problem sorting the array"
Resume NormalExit
End Sub
See How do I use VBA code in PowerPoint? to learn how to use this example code.