Sorts the collection of items by the specified property. The index for the collection is reset to 1 upon completion of this method.
expression.Sort(Property, Descending, Order)
expression Required. An expression that returns one of the objects in the Applies To list.
Property Required String. The name of the property by which to sort, which may be enclosed in brackets (for example, "[CompanyName]"). May not be a user-defined field, and may not be a multi-valued property, such as a category.
Descending Applies to all objects in the Applies To list except the AddressEntries object. Optional Variant for the Results object; optional Boolean for all other objects. True to sort in descending order. The default value is False (ascending).
Order Applies to the AddressEntries object only. Optional Variant. The order for the specified address entries. Can be one of these OlSortOrder constants: olAscending, olDescending, or olSortNone.
For the Items collection, Sort cannot be used and will cause an error with the following properties:
Sort only affects the order of items in a collection. It does not affect the order of items in an explorer view.
The following Visual Basic for Applications (VBA) example uses the Sort method to sort the Items collection for the default Tasks folder by the "DueDate" property and displays the due dates each in turn.
Sub SortByDueDate() Dim myOlApp As New Outlook.Application Dim myNameSpace As Outlook.NameSpace Dim myFolder As Outlook.MAPIFolder Dim myItem As Outlook.TaskItem Dim myItems As Outlook.Items Set myNameSpace = myOlApp.GetNamespace("MAPI") Set myFolder = myNameSpace.GetDefaultFolder(olFolderTasks) Set myItems = myFolder.Items myItems.Sort "[DueDate]", False For Each myItem In myItems MsgBox myItem.Subject & "-- " & myItem.DueDate Next myItem End Sub
If you use Microsoft Visual Basic Scripting Edition (VBScript) in a Microsoft Outlook form, you do not create the Application object, and you cannot use named constants. This example shows how to sort the Contacts folder by CompanyName property using VBScript.
Sub CommandButton1_Click() Set myNamespace = Application.GetNamespace("MAPI") Set myFolder = _ myNameSpace.GetDefaultFolder(10) Set myItems = myFolder.Items myItems.Sort "[CompanyName]", False For Each myItem in myItems MsgBox myItem.CompanyName Next End Sub