开发者

WORD VBA sorting words in paragraphs

Hi guys i am hoping that you guys can point me in the right direction. I am trying to come up with a macro that will sort words in ascending order for each paragraph. T开发者_开发百科o make it clear i am giving an example below:

The quick brown fox jumps over the lazy dog. <---- given
brown dog fox jumps lazy over quick the the <---- the output

The output should show below the para/s right at the end of the docu already sorted. Any help or advice as to how i can go about it using Ranges, pls let me know. thanks!


I believe this code might point you in the right direction.

Notice you'll need to add somewhere the sorted array (as of now, it's only sorting the array values).

I used the sorting function available HERE.

Hope it helps!

Option Explicit
Option Compare Text

Sub orderParagraph()

    Dim oParagraph As Word.Paragraph
    Dim vParagraphText As Variant

    For Each oParagraph In ActiveDocument.Paragraphs

        If Len(Trim(oParagraph.Range.Text)) > 0 Then

            vParagraphText = oParagraph.Range.Text
            vParagraphText = Split(vParagraphText, " ")
            SortArray vParagraphText

        End If

    Next oParagraph


End Sub

Private Function SortArray(ByRef TheArray As Variant)

    Dim x As Integer
    Dim bSorted As Boolean
    Dim sTempText As String

    bSorted = False

    Do While Not bSorted

        bSorted = True

        For x = 0 To UBound(TheArray) - 1

            If TheArray(x) > TheArray(x + 1) Then

                sTempText = TheArray(x + 1)
                TheArray(x + 1) = TheArray(x)
                TheArray(x) = sTempText
                bSorted = False

            End If

        Next x

    Loop

End Function
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜