ajruiz28
02-10-2012, 08:59 AM
This is my first post so please bear with me. I am trying to sort slides in a presentation based on the number value that I get from the NotesPage section of each slide. All slides have been preset with a number that I extract using a function in VBA. I have an array that contains these numbers and I then sort them using a bubble sort routine. Once the sort is finished I attempt to move the slides to the end. The problem is that it doesn't sort it properly. The numbers in the NotesPage section of the slides can range from 1 to 10,000. I want the sort routine to order the slides from lowest to highest, but can't seem to get it working! Please help. I have posted the current code below and if you want to test this out you must add a number value in the NotesPage section of the slides to be sorted in the format: {10}. My function parses the value between the brackets {}. All the code is behind slide 1 and is run from a button click. Any feedback is much appreciated!
Option Explicit
'
Private Sub cmdSort_Click()
'Sorts the slides based on the notesPage value ex: {1}
Dim x As Long
Dim raySlides() As Variant
Dim lIndex As Long
ReDim Preserve raySlides(1 To ActivePresentation.Slides.Count) As Variant
Dim sld As Slide
Dim notes As String
'Set array values
For x = 1 To ActivePresentation.Slides.Count
notes = ParseSlideNumber(ActivePresentation.Slides(x).NotesPage.Shapes.Placeholders (2).TextFrame.TextRange.Text)
raySlides(x) = notes & "|||" & CStr(x)
Next x
'Sort the array
Call BubbleSortVariantArray(raySlides())
lIndex = 1
For x = 1 To UBound(raySlides)
lIndex = CLng(Mid$(raySlides(x), InStr(raySlides(x), "|||") + 3))
ActivePresentation.Slides(lIndex).MoveTo ActivePresentation.Slides.Count
Next x
'Move master slide to 1st position
For Each sld In ActivePresentation.Slides
If ParseSlideNotes(sld.NotesPage.Shapes.Placeholders(2).TextFrame.TextRange.Te xt) = "Master" Then
ActivePresentation.Slides(sld.SlideIndex).MoveTo 1
End If
Next sld
MsgBox "Finished Sorting Slides!", vbInformation, "Sort Slides"
End Sub
Private Function ParseSlideNumber(notes As String) As Integer
'Parses the notes string and retrieves the value between brackets
Dim leftBracket As Integer
Dim rightBracket As Integer
leftBracket = InStr(1, notes, "{")
rightBracket = InStr(1, notes, "}")
If leftBracket > 0 And rightBracket > 0 Then
ParseSlideNumber = CInt(Mid(notes, leftBracket + 1, rightBracket - 1 - leftBracket))
Exit Function
End If
ParseSlideNumber = 0
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
Option Explicit
'
Private Sub cmdSort_Click()
'Sorts the slides based on the notesPage value ex: {1}
Dim x As Long
Dim raySlides() As Variant
Dim lIndex As Long
ReDim Preserve raySlides(1 To ActivePresentation.Slides.Count) As Variant
Dim sld As Slide
Dim notes As String
'Set array values
For x = 1 To ActivePresentation.Slides.Count
notes = ParseSlideNumber(ActivePresentation.Slides(x).NotesPage.Shapes.Placeholders (2).TextFrame.TextRange.Text)
raySlides(x) = notes & "|||" & CStr(x)
Next x
'Sort the array
Call BubbleSortVariantArray(raySlides())
lIndex = 1
For x = 1 To UBound(raySlides)
lIndex = CLng(Mid$(raySlides(x), InStr(raySlides(x), "|||") + 3))
ActivePresentation.Slides(lIndex).MoveTo ActivePresentation.Slides.Count
Next x
'Move master slide to 1st position
For Each sld In ActivePresentation.Slides
If ParseSlideNotes(sld.NotesPage.Shapes.Placeholders(2).TextFrame.TextRange.Te xt) = "Master" Then
ActivePresentation.Slides(sld.SlideIndex).MoveTo 1
End If
Next sld
MsgBox "Finished Sorting Slides!", vbInformation, "Sort Slides"
End Sub
Private Function ParseSlideNumber(notes As String) As Integer
'Parses the notes string and retrieves the value between brackets
Dim leftBracket As Integer
Dim rightBracket As Integer
leftBracket = InStr(1, notes, "{")
rightBracket = InStr(1, notes, "}")
If leftBracket > 0 And rightBracket > 0 Then
ParseSlideNumber = CInt(Mid(notes, leftBracket + 1, rightBracket - 1 - leftBracket))
Exit Function
End If
ParseSlideNumber = 0
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