Sort Method

Microsoft Word Visual Basic

Show All

Sort Method

       

Sort method as it applies to the Column object.

Sorts the specified table column.

expression.Sort(ExcludeHeader, SortFieldType, SortOrder, CaseSensitive, BidiSort, IgnoreThe, IgnoreKashida, IgnoreDiacritics, IgnoreHe, LanguageID)

expression   Required. An expression that returns a Column object.

ExcludeHeader  Optional Variant. True to exclude the first row or paragraph header from the sort operation. The default value is False.

SortFieldType  Optional Variant. The sort type for the column. Can be one of the WdSortFieldType constants.

wdSortFieldAlphanumeric Default
wdSortFieldDate
wdSortFieldJapanJIS
wdSortFieldKoreaKS
wdSortFieldNumeric
wdSortFieldStroke
wdSortFieldSyllable

SortOrder  Optional Variant. The sorting order to use for the column. Can be one WdSortOrder constant.

wdSortOrderAscending Default
wdSortOrderDescending

CaseSensitive  Optional Variant. True to sort with case sensitivity. The default value is False.

BidiSort  Optional Variant. True to sort based on right-to-left language rules. This argument may not be available to you, depending on the language support (U.S. English, for example) that you’ve selected or installed.

IgnoreThe  Optional Variant. True to ignore the Arabic character alef lam when sorting right-to-left language text. This argument may not be available to you, depending on the language support (U.S. English, for example) that you’ve selected or installed.

IgnoreKashida  Optional Variant. True to ignore kashidas when sorting right-to-left language text. This argument may not be available to you, depending on the language support (U.S. English, for example) that you’ve selected or installed.

IgnoreDiacritics  Optional Variant. True to ignore bidirectional control characters when sorting right-to-left language text. This argument may not be available to you, depending on the language support (U.S. English, for example) that you’ve selected or installed.

IgnoreHe  Optional Variant. True to ignore the Hebrew character he when sorting right-to-left language text. This argument may not be available to you, depending on the language support (U.S. English, for example) that you’ve selected or installed.

LanguageID  Optional Variant. Optional VariantLanguageID  Optional Variant. Specifies the sorting language. Can be one of the WdLanguageID constants. Refer to the Object Browser for a list of the WdLanguageID constants.

Remarks

If you want to sort paragraphs within a table cell, include only the paragraphs and not the end-of-cell mark; if you include the end-of-cell mark in a selection or range and then attempt to sort the paragraphs, Word displays a message stating that it found no valid records to sort.

Sort method as it applies to the Range and Selection objects.

Sorts the paragraphs in the specified range or selection.

expression.Sort(ExcludeHeader, FieldNumber, SortFieldType, SortOrder, FieldNumber2, SortFieldType2, SortOrder2, FieldNumber3, SortFieldType3, SortOrder3, SortColumn, Separator, CaseSensitive, BidiSort, IgnoreThe, IgnoreKashida, IgnoreDiacritics, IgnoreHe, LanguageID)

expression   Required. An expression that returns one of the above objects.

ExcludeHeader  Optional Variant. True to exclude the first row or paragraph header from the sort operation. The default value is False.

FieldNumber, FieldNumber2, FieldNumber3  Optional Variant. The fields to sort by. Microsoft Word sorts by FieldNumber, then by FieldNumber2, and then by FieldNumber3.

SortFieldType, SortFieldType2, SortFieldType3  Optional Variant. The respective sort types for FieldNumber, FieldNumber2, and FieldNumber3. Can be one of the WdSortFieldType constants.

wdSortFieldAlphanumeric
wdSortFieldDate
wdSortFieldJapanJIS
wdSortFieldKoreaKS
wdSortFieldNumeric
wdSortFieldStroke
wdSortFieldSyllable

The default value is wdSortFieldAlphanumeric. Some of these constants may not be available to you, depending on the language support (U.S. English, for example) that you’ve selected or installed.

SortOrder, SortOrder2, SortOrder3  Optional Variant. The sorting order to use when sorting FieldNumber, FieldNumber2, and FieldNumber3. Can be one WdSortOrder constant.

wdSortOrderAscending Default.
wdSortOrderDescending

SortColumn  Optional Variant. True to sort only the column specified by the Range or Selection object.

Separator  Optional Variant. The type of field separator. Can be one of the WdSortSeparator constants.

wdSortSeparateByCommas Default.
wdSortSeparateByDefaultTableSeparator
wdSortSeparateByTabs

CaseSensitive  Optional Variant. True to sort with case sensitivity. The default value is False.

BidiSort  Optional Variant. True to sort based on right-to-left language rules. This argument may not be available to you, depending on the language support (U.S. English, for example) that you’ve selected or installed.

IgnoreThe  Optional Variant. True to ignore the Arabic character alef lam when sorting right-to-left language text. This argument may not be available to you, depending on the language support (U.S. English, for example) that you’ve selected or installed.

IgnoreKashida  Optional Variant. True to ignore kashidas when sorting right-to-left language text. This argument may not be available to you, depending on the language support (U.S. English, for example) that you’ve selected or installed.

IgnoreDiacritics  Optional Variant. True to ignore bidirectional control characters when sorting right-to-left language text. This argument may not be available to you, depending on the language support (U.S. English, for example) that you’ve selected or installed.

IgnoreHe  Optional Variant. True to ignore the Hebrew character he when sorting right-to-left language text. This argument may not be available to you, depending on the language support (U.S. English, for example) that you’ve selected or installed.

LanguageID  Optional Variant. LanguageID  Optional Variant. Specifies the sorting language. Can be one of the WdLanguageID constants. Refer to the Object Browser for a list of the WdLanguageID constants.

SubFieldNumber, SubFieldNumber2, SubFieldNumber3  Optional Variant. (Applies to the Selection object only.)

Sort method as it applies to the Table object.

Sorts the specified table.

expression.Sort(ExcludeHeader, FieldNumber, SortFieldType, SortOrder, FieldNumber2, SortFieldType2, SortOrder2, FieldNumber3, SortFieldType3, SortOrder3, CaseSensitive, BidiSort, IgnoreThe, IgnoreKashida, IgnoreDiacritics, IgnoreHe, LanguageID)

expression   Required. An expression that returns a Table object.

ExcludeHeader  Optional Variant. True to exclude the first row or paragraph header from the sort operation. The default value is False.

FieldNumber, FieldNumber2, FieldNumber3  Optional Variant. The fields to sort by. Microsoft Word sorts by FieldNumber, then by FieldNumber2, and then by FieldNumber3.

wdSortFieldAlphanumeric
wdSortFieldDate
wdSortFieldJapanJIS
wdSortFieldKoreaKS
wdSortFieldNumeric
wdSortFieldStroke
wdSortFieldSyllable

The default value is wdSortFieldAlphanumeric. Some of these constants may not be available to you, depending on the language support (U.S. English, for example) that you’ve selected or installed.

SortOrder, SortOrder2, SortOrder3  Optional Variant. The sorting order to use when sorting FieldNumber, FieldNumber2, and FieldNumber3. Can be one WdSortOrder constant.

wdSortOrderAscending Default.
wdSortOrderDescending

CaseSensitive  Optional Variant. True to sort with case sensitivity. The default value is False.

BidiSort  Optional Variant. True to sort based on right-to-left language rules. This argument may not be available to you, depending on the language support (U.S. English, for example) that you’ve selected or installed.

IgnoreThe  Optional Variant. True to ignore the Arabic character alef lam  when sorting right-to-left language text. This argument may not be available to you, depending on the language support (U.S. English, for example) that you’ve selected or installed.

IgnoreKashida  Optional Variant. True to ignore kashidas when sorting right-to-left language text. This argument may not be available to you, depending on the language support (U.S. English, for example) that you’ve selected or installed.

IgnoreDiacritics  Optional Variant. True to ignore bidirectional control characters when sorting right-to-left language text. This argument may not be available to you, depending on the language support (U.S. English, for example) that you’ve selected or installed.

IgnoreHe  Optional Variant. True to ignore the Hebrew character he when sorting right-to-left language text. This argument may not be available to you, depending on the language support (U.S. English, for example) that you’ve selected or installed.

LanguageID  Optional Variant. Specifies the sorting language. Can be one of the WdLanguageID constants. Refer to the Object Browser for a list of the WdLanguageID constants.

Example

As it applies to the Table object.

This example sorts the first table in the active document, excluding the heading row.

Sub NewTableSort()
    ActiveDocument.Tables(Index:=1)
    Selection.Sort ExcludeHeader:=True
End Sub

As it applies to the Range or Selection object.

This example inserts three lines of text into a new document and then sorts the lines in ascending alphanumeric order

Sub NewParagraphSort()
    Dim newDoc As Document
    Set newDoc = Documents.Add
    newDoc.Content.InsertAfter "pear" & Chr(13) _
        & "zucchini" & Chr(13) & "apple" & Chr(13)
    newDoc.Content.Sort SortOrder:=wdSortOrderAscending
End Sub