Sort Method 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