Could you please help with writing a VB or VBA code for permutations:

Context:

There are some sentences in Word that will be selected by a user. The result
should be a permutation (only one, not all of them) of these sentences (i.e.,
sent4, sent2, sent1, sent3 instead of the initial sent1, sent2, sent3, sent4).
Thank you in advance for every help.

Re: VB or VBA code for permutations by Jean-Guy

Jean-Guy
Wed Sep 21 11:18:19 CDT 2005

Leisureman was telling us:
Leisureman nous racontait que :

> Could you please help with writing a VB or VBA code for permutations:

What have you got so far?

> Context:
>
> There are some sentences in Word that will be selected by a user. The

How do you define a "sentence" for your purpose?

> result should be a permutation (only one, not all of them) of these

But in your example you permute more than one sentence. So, which is it?

> sentences (i.e., sent4, sent2, sent1, sent3 instead of the initial
> sent1, sent2, sent3, sent4). Thank you in advance for every help.

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.


--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org



Re: VB or VBA code for permutations by Jay

Jay
Wed Sep 21 13:43:20 CDT 2005

Jean-Guy Marcil wrote:
> Leisureman was telling us:
> Leisureman nous racontait que :
>
>> Could you please help with writing a VB or VBA code for permutations:
>
> What have you got so far?
>
>> Context:
>>
>> There are some sentences in Word that will be selected by a user. The
>
> How do you define a "sentence" for your purpose?
>
>> result should be a permutation (only one, not all of them) of these
>
> But in your example you permute more than one sentence. So, which is
> it?
>
>> sentences (i.e., sent4, sent2, sent1, sent3 instead of the initial
>> sent1, sent2, sent3, sent4). Thank you in advance for every help.
>
> 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

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org