Microsoft KB Archive/183053

= OL98: Collection of Items Does Not Appear to Sort =

Article ID: 183053

Article Last Modified on 2/27/2004

-

APPLIES TO


 * Microsoft Outlook 98 Standard Edition

-



This article was previously published under Q183053



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://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS

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.

