2011-03-17 144 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