DALLASCAO.COM

Site of Cao Shouguang, English to Chinese translator

Word VBA: randomize paragraphs

Sub ran_para()
    n = ActiveDocument.Paragraphs.Count
    ReDim a(1 To 2, 1 To n)
    Randomize
    For i = 1 To n
        a(1, i) = Rnd
        a(2, i) = i
    Next
    For i = 1 To n - 1
        For j = i + 1 To n
            If a(1, j) > a(1, i) Then
                t = a(2, i)
                a(2, i) = a(2, j)
                a(2, j) = t
            End If
        Next
    Next
    'Documents.Add
    For i = 1 To n
        Set p = ActiveDocument.Paragraphs.Add
        p.Range.Text = ActiveDocument.Paragraphs(a(2, i)).Range.Text
    Next
End Sub


Leave a Comment