2011-03-17 142 views
0

嗨,大家好,我希望你们可以指出我正确的方向。我试图想出一个宏,它会按照每个段落的升序对文字进行排序。为了说清楚我给出的例子如下: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

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

注意,您需要添加排序数组的某个地方(截至目前,它只对数组值进行排序)。

我用排序功能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 
+0

的确有很大的帮助!谢啦!是不是可以将值存储在数组中,将它们显示在列表样式的底部,然后应用单词的内置排序方法?例如。 expression.Sort(parameters) – decrementor 2011-03-18 06:16:23

+0

此外,如何在不包含句点,逗号等的情况下完成这个工作,它应该只提取文本。任何帮助? – decrementor 2011-03-21 06:28:48

+0

@decrementor,只需在split命令之前添加这两行即可:vParagraphText = Replace(vParagraphText,“。”,“”) vParagraphText = Replace(vParagraphText,“,”,“”) – 2011-03-21 10:50:01