Jean-Guy Marcil said:
Leisureman was telling us:
Leisureman nous racontait que :
What have you got so far?
How do you define a "sentence" for your purpose?
But in your example you permute more than one sentence. So, which is
it?
But, those questions become unimportant if the sentences selected by
the user are not contiguous (one after the other) because Word VBA
does not allow you to work with non-contiguous selection.
Once you've figured out what data you're working with, the general idea of
permuting it works like this:
- Create an array containing the data items (your sentences).
- For each index from 1 to the size of the array, select another index at
random and swap the contents at those two indexes.
- Print out the array in its new order.
Here's a sample macro:
Sub PermutationExample()
Dim MyArray As Variant
Dim Index As Integer
Dim OtherIndex As Integer
Dim TempStr As String
Dim msg As String
' initialize -- you'll have to do this differently
MyArray = Array("A", "B", "C", "D", "E", "F")
Randomize
' show starting state
For Index = 0 To UBound(MyArray)
msg = msg & MyArray(Index)
Next Index
MsgBox "Starting array: " & msg
' PERMUTE -- the important part!
For Index = 0 To UBound(MyArray)
' pick a random partner
OtherIndex = Rnd() * UBound(MyArray)
' swap it with the current entry
TempStr = MyArray(OtherIndex)
MyArray(OtherIndex) = MyArray(Index)
MyArray(Index) = TempStr
Next Index
' show ending state
msg = ""
For Index = 0 To UBound(MyArray)
msg = msg & MyArray(Index)
Next Index
MsgBox "Ending array: " & msg
End Sub