WORD VBA在段落中排序单词

时间:2011-03-17 07:50:19

标签: vba ms-word word-vba

大家好,我希望你们能指出我正确的方向。我试图想出一个宏,它将按每个段落的升序排序单词。为了说清楚,我在下面给出一个例子:

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

输出应显示在已排序的文档末尾的para / s右下方。关于如何使用Ranges进行操作的任何帮助或建议,请告诉我。谢谢!

1 个答案:

答案 0 :(得分:1)

我相信这段代码可能会指出你正确的方向。

请注意,您需要在已排序的数组中添加某个位置(截至目前,它只对数组值进行排序)。

我使用了可用的排序功能HERE

希望它有所帮助!

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