• Randomizing sentences (N/A)

    Author
    Topic
    #419899

    This is a bit of an odd request, but hey, I’m sure if someone asked me about it, there are other people out there who might need something like this too…

    My friend’s mother is a teacher’s aide (Im assuming this is relevant) and asked me if I knew of a program/utility to take a document and randomly move the sentences around within the document.

    Further assumption: I guess it’s test questions or something, and they want to mix up the order of the quiz/exam without spending hours and hours of cutting and pasting or re-typing.

    Has anyone heard of something that does this, or is it something to be done with VBA and a custom little scriptlet-type of thing?

    TIA

    Viewing 0 reply threads
    Author
    Replies
    • #949325

      There are some subtle parts of this problem that unfortunately I don’t have time to explore right now, but perhaps this will help you or another lounger get to a useful solution.

      The following macro (which uses three supporting functions) makes a list of random numbers between 1 and the number of sentences in the document. Then a new document is created, and the sentences from the original are copied over in that random order.

      The subtleties I was talking about:
      1. Word’s definition of a “sentence” is not always the same as yours.
      2. The trailing paragraph mark at the end of the last sentence in a paragraph is considered part of that sentence, so your paragraph breaks get a bit scrambled.
      3. I know not enough about randomization to know if this is truly “random” enough for your purposes.

      At any rate, it’s an intriguing litte puzzle, and I hope I’ve put the first few pieces in place.

      Sub ShuffleSentencesInDoc()
      Dim doc As Document
      Dim docShuffled As Document
      Dim lRandNum As Long
      Dim k As Long
      Dim lSentenceCount As Long
      Dim vRand() As Variant
      ReDim vRand(0)
      Set doc = ActiveDocument
      lSentenceCount = doc.Sentences.count
      Do While UBound(vRand) < doc.Sentences.count
        lRandNum = GenerateBoundedRandomNumber(1, lSentenceCount)
        If Not IsMember(vRand, lRandNum) Then
          Push vRand, lRandNum
        End If
      Loop
      Set docShuffled = Documents.Add
      For k = 1 To lSentenceCount
          docShuffled.Range.InsertAfter doc.Sentences(vRand(k)).Text
      Next k
      End Sub
      
      Function GenerateBoundedRandomNumber(lLbound As Long, lUbound As Long) As Long
      Randomize
      GenerateBoundedRandomNumber = Int((lUbound - lLbound + 1) * Rnd + lLbound)
      End Function
      
      Function Push(ByRef vArray As Variant, ByVal vItem As Variant)
      ReDim Preserve vArray(UBound(vArray) + 1)
      vArray(UBound(vArray)) = vItem
      End Function
      
      Function IsMember(vArray As Variant, ByVal vItem As Variant)
      Dim v As Variant
      For Each v In vArray
          If v = vItem Then
              IsMember = True
              Exit Function
          End If
      Next v
      IsMember = False
      End Function
      
      • #949334

        Well, it should certainly be a start, anyway! thanks for the quick reply! When I get home from work tonight I’ll see if I can glean more details from the recipient and see what’s what… I think the biggest problem is exactly how you worded it: the difference between what I think is a sentence and what Word thinks is a sentence.

        Thanks!

    Viewing 0 reply threads
    Reply To: Randomizing sentences (N/A)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: