randomising word order in a document

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

i would like to arrange words in a document alphabetically or randomly - can
anyone tell me how to do this ?
 
Suzanne,

Here are a couple macros that might get you started. The first evaluates
and existing document, then creates a new document with the words in
alphabetical order. I have stripped out some puncuation.

The second randomize words in a document (not sure about size limitations).

Sub AlphaOrderWords()
Dim SourceDoc As Document
Dim TargetDoc As Document
Dim awords As Integer
Dim aword As Range
Dim countwords As Integer

Set SourceDoc = ActiveDocument
countwords = 1
awords = ActiveDocument.Words.Count
Set TargetDoc = Documents.Add
SourceDoc.Activate
Do Until countwords = awords
Set aword = SourceDoc.Words(1)
Select Case aword
Case Is = vbCr
aword.Delete
Case Is <> vbCr
If InStr(". ? ! : ; ", aword) = 1 Then
aword.Delete
Else
TargetDoc.Range.InsertAfter aword & vbCr
aword.Delete
End If
Case Else
GoTo SortTarget
End Select
awords = ActiveDocument.Words.Count
Loop
SortTarget:
TargetDoc.Range.Sort ExcludeHeader:=False, _
FieldNumber:="Paragraphs", _
SortFieldType:=wdSortFieldAlphanumeric, _
SortOrder:=wdSortOrderAscending
Set aword = TargetDoc.Range
With aword.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "^p"
.Replacement.Text = " "
.Execute Replace:=wdReplaceAll
.Text = " "
.Replacement.Text = " "
.Execute Replace:=wdReplaceAll
End With
SourceDoc.Close wdDoNotSaveChanges

End Sub

Sub RandomizeWords()
Dim wordArray() As String
Dim tempStr As String
Dim i As Long
Dim j As Long

wordArray = Split(ActiveDocument.Range.Text)
Randomize
For i = 0 To UBound(wordArray)
j = Rnd * UBound(wordArray)
tempStr = wordArray(i)
wordArray(i) = wordArray(j)
wordArray(j) = tempStr
Next i
ActiveDocument.Range.Text = Join(wordArray)
End Sub
 
Another approach is to copy your word list into Excel, sort there, and copy
back. Alphabetic order is obviously easy. To randomize, insert a random
number in each row, sort on that, then read back the words.
 
thank you for your response but i'm afraid I'm not technical enough for
macros! thanks for your help though.

Suzanne
 
Back
Top