OL98: Collection of Items Does Not Appear to Sort

ID: Q183053

The information in this article applies to:

SYMPTOMS

You use the Sort method example in the Microsoft Outlook Visual Basic Reference (Vbaoutl.hlp) and items do not appear to be sorted in the collection.

CAUSE

While the example in the Help file does actually sort the collection, it then refetches the collection and therefore the items do not appear to be sorted.

STATUS

Microsoft has confirmed this to be a problem in Microsoft Outlook 98.

MORE INFORMATION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft Support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

   http://www.microsoft.com/support/supportnet/refguide/default.asp

Use the following Visual Basic for Applications automation code example as a basis for how to work with sorted collections:

   Sub SortContacts()
      Set ol = New Outlook.Application
      Set olns = ol.GetNameSpace("MAPI")
      Set MyFolder = olns.GetDefaultFolder(olFolderContacts)
      ' Create a collection of items
      Set MyItems = MyFolder.Items
      ' Sort the collection
      MyItems.Sort "[CompanyName]", False
      '  Loop through the sorted collection
      For Each MyItem in MyItems
         MsgBox MyItem.CompanyName
      Next MyItem
   End Sub

For comparative purposes, the incorrect approach provided in the Help file is:

   Sub SortContacts()
      Set ol = New Outlook.Application
      Set olns = ol.GetNameSpace("MAPI")
      Set MyFolder = olns.GetDefaultFolder(olFolderContacts)
      MyFolder.Items.Sort "[CompanyName]", False
      ' MyFolder.Items causes Outlook to re-fetch the collection.
      For Each MyItem in MyFolder.Items
         MsgBox MyItem.CompanyName
      Next MyItem
   End Sub

NOTE: This problem also existed in the Outlook 97 version of this Help file.

REFERENCES

For more information about creating solutions with Microsoft Outlook 98, please see the following articles in the Microsoft Knowledge Base:

   Article-ID: Q180826
   Title     : OL98: Resources for Custom Forms and Programming

   Article-ID: Q182349
   Title     : OL98: Questions About Custom Forms and Outlook Solutions

Additional query words: OutSol OutSol98
Keywords          : kbcode 
Version           : WINDOWS:98
Platform          : WINDOWS

Last Reviewed: May 17, 1999