Sort Method
Collapse the table of content
Expand the table of content
The document is archived and information here might be outdated

Items.Sort Method

Office 2007
Sorts the collection of items by the specified property. The index for the collection is reset to 1 upon completion of this method.

Syntax

expression.Sort(Property, Descending)

expression   A variable that represents an Items object.

Parameters

NameRequired/OptionalData TypeDescription
PropertyRequiredStringThe name of the property by which to sort, which may be enclosed in brackets, for example, "[CompanyName]". User-defined properties that contain spaces must be enclosed in brackets. May not be a user-defined property of type keywords, and may not be a multi-valued property, such as a category. For user-defined properties, the property must exist in the UserDefinedProperties collection for Items.Parent, which represents the Folder object that contains the items.
DescendingOptionalVariantTrue to sort in descending order. The default value is False (ascending).

Remarks

Sort only affects the order of items in a collection. It does not affect the order of items in an explorer view.

Sort cannot be used and will cause an error if the

property paramater is one of the following properties:

Example

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.

Visual Basic for Applications
Sub SortByDueDate()
    Dim myNameSpace As Outlook.NameSpace
    Dim myFolder As Outlook.Folder
    Dim myItem As Outlook.TaskItem
    Dim myItems As Outlook.Items

    Set myNameSpace = Application.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



Show:
© 2016 Microsoft