Sorting in Microsoft Word using VBA (Visual Basic for Applications)

Sorting in Microsoft Word using VBA (Visual Basic for Applications)

Sorting

Do you know, Microsoft Word has capability to perform sorting over a document. In this article we will write code to sort paragraphs in Alphabetical order using visual basic for applications code.

Before code

Code example

Sub sortText()
Selection.Sort ExcludeHeader:=False, FieldNumber:="Paragraphs", _
        SortFieldType:=wdSortFieldAlphanumeric, SortOrder:=wdSortOrderAscending, _
        FieldNumber2:="", SortFieldType2:=wdSortFieldAlphanumeric, SortOrder2:= _
        wdSortOrderAscending, FieldNumber3:="", SortFieldType3:= _
        wdSortFieldAlphanumeric, SortOrder3:=wdSortOrderAscending, Separator:= _
        wdSortSeparateByTabs, SortColumn:=False, CaseSensitive:=False, LanguageID _
        :=wdEnglishUS, SubFieldNumber:="Paragraphs", SubFieldNumber2:= _
        "Paragraphs", SubFieldNumber3:="Paragraphs"
End Sub

Put above code in a module, Select number of paragraphs that you would like to sort in A-Z order and press F5 key to execute.

Final Output

Leave a Reply

Your email address will not be published.