【问题标题】:WORD VBA sorting words in paragraphsWORD VBA 对段落中的单词进行排序
【发布时间】:2011-03-17 23:15:22
【问题描述】:

大家好,我希望你们能指出我正确的方向。我正在尝试提出一个宏,该宏将按每个段落的升序对单词进行排序。为了清楚起见,我在下面举一个例子:

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

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

【问题讨论】:

    标签: vba ms-word


    【解决方案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
    

    【讨论】:

    • 确实有很大帮助!谢啦!不能将值存储在数组中,以列表样式显示在底部,然后应用 word 的内置排序方法吗?例如。表达式.排序(参数)
    • 另外,如何在不包括句点、逗号等的情况下完成此操作。它应该只提取文本。有什么帮助吗?
    • @decrementor,只需在 split 命令之前添加这两行: vParagraphText = Replace(vParagraphText, ".", "") vParagraphText = Replace(vParagraphText, ",", "")
    猜你喜欢
    • 2021-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多