Sort Method
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.
Remarks
For the Items collection, Sort cannot be used, and will cause an error, with the following properties:
Categories
Children Class Companies CompanyLastFirstNoSpace CompanyLastFirstSpaceOnly Contacts DLName IsOnlineMeeting LastFirstAndSuffix LastFirstNoSpace LastFirstNoSpaceCompany |
LastFirstSpaceOnly
LastFirstSpaceOnlyCompany MemberCount NetMeetingAlias NetMeetingAutoStart NetMeetingOrganizerAlias NetMeetingServer NetMeetingType RecurrenceState ResponseState Sent Saved |
Sort only affects the order of items in a collection. It does not affect the order of items in an explorer view.
Example
The following Visual Basic for Applications example uses the Sort method to sort the Items collection for the default Contacts folder by the "CompanyName" property and then displays the company names each in turn.
Set myOlApp = CreateObject("Outlook.Application")
Set myNamespace = myOlApp.GetNamespace("MAPI")
Set myFolder = _
myNameSpace.GetDefaultFolder(olFolderContacts)
Set myItems = myFolder.Items
myItems.Sort "[CompanyName]", False
For Each myItem in myItems
MsgBox myItem.CompanyName
Next myItem
If you use VBScript, you do not create the Application object, and you cannot use named constants. This example shows how to perform the same task using VBScript.
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