WORD VBA 对段落中的单词进行排序

发布于 2024-10-24 08:29:58 字数 281 浏览 1 评论 0原文

大家好,我希望你们能为我指出正确的方向。我试图想出一个宏,可以按升序对每个段落的单词进行排序。为了清楚起见,我在下面给出了一个示例:

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

输出应显示在已排序的文档末尾的段落下方。有关如何使用范围进行操作的任何帮助或建议,请告诉我。谢谢!

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. To 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

古镇旧梦 2024-10-31 08:29:58

我相信这段代码可能会为您指明正确的方向。

请注意,您需要在某处添加已排序的数组(到目前为止,它仅对数组值进行排序)。

我使用了此处可用的排序功能。

希望有帮助!

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文