OL2000: How to Sort a List Box or Combo Box

ID: Q227178


The information in this article applies to:
  • Microsoft Outlook 2000


SUMMARY

In Microsoft Outlook, there is no direct, or built-in, way to sort the contents of a list box or combo box control. This article provides two examples that use Visual Basic Scripting Edition (VBScript) code to sort information.


MORE INFORMATION

NOTE: For simplicity, the term list box will be used to denote either a list box or a combo box control.

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 a Microsoft Certified Solution Provider or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Solution Providers, please see the following page on the World Wide Web:

http://www.microsoft.com/mcsp/
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/overview/overview.asp

Using the Items.Sort Method

This method assumes that you are populating the list box with information from items in an Outlook folder. If so, you can use the Items.Sort method to sort the items based on a particular field, to loop through those sorted items, and to insert the values of the field into the list box.

This example will add a list box to a Task form and populate the list box with contacts from the default Contacts folder. The contacts will be sorted by the Full Name field.
  1. Open a new Task item.


  2. On the Tools menu, click Forms, and then click Design This Form.


  3. Click the (P.2) tab.


  4. On the Form menu, click Control Toolbox. On the Control Toolbox, click and drag a ListBox onto the form.


  5. Right-click the list box and then click Properties. On the Value page, click New....Type TestLB for the name and click OK. Click OK again.


  6. On the Form menu, click View Code.


  7. In the Script Editor, type the following VBScript code:



  8. 
    Sub Item_Open()
    
       Set objFormPage = Item.GetInspector.ModifiedFormPages("P.2")
       Set objListBox = objFormPage.Controls("ListBox1")
     
       Set objNS = Application.GetNamespace("MAPI")<BR/>
       ' Get the default Contacts folder
       Set objConFolder = objNS.GetDefaultFolder(10) ' olFolderContacts
    
       ' Get the items in the folder
       Set colConItems = objConFolder.Items
    
       ' Create a Restrict filter
       strFilter = "[FullName] <> " & Chr(34) & Chr(34)
    
       ' Just get those contacts with a Full Name
       Set colResItems = colConItems.Restrict(strFilter)
    
       ' Sort the contacts based on FullName
       colResItems.Sort "[FullName]"
    
       ' Loop through all of the sorted contacts
       For Each objItem in colResItems
          ' Add the name to the listbox
          objListBox.AddItem objItem.FullName
       Next
       ' Loop through all of the sorted contacts
       For Each objItem in colResItems
          ' Add the name to the listbox and ignore distribution lists
          If Left(objItem.MessageClass, 11) = "IPM.Contact" Then
             objListBox.AddItem objItem.FullName
          End If
       Next
    
    End Sub 
  9. On the Form menu, click Run this Form.


  10. Click the P.2 page of the form.


The Item_Open event will run, and then the VBScript code will sort the contacts and load the list box on the P.2 page.

Use a Sorting Algortim

Sort algoritms can be used to sort any list. In the following example, the values in the list box are predefined and sorted using the Bubble Sort algoritm. Note that these values could also be retrieved from another source, such as an external database application.
  1. On the Tools menu, click Forms, and then click Design a Form.


  2. In the Standard Forms Library, click Message, and then click Open.


  3. Click the (P.2) tab.


  4. On the Form menu, click Control Toolbox. On the Control Toolbox, click and drag a ListBox onto the form.


  5. Right-click the list box, and then click Properties. On the Value page, click New.... Type TestLB for the name, and click OK. In the Possible Values field, type Dog;Cat;Mouse;Horse;Pig;Cow;Chicken;Rabbit, and then click OK.


  6. On the Control Toolbox, click and drag a CommandButton onto the form.


  7. Right-click the command button, and then click Properties. Click the Display tab, type Sort for the Caption, and then click OK.


  8. On the Form menu, click View Code.


  9. In the Script Editor, type the following VBScript code:



  10. 
    Sub CommandButton1_Click()
       Dim strArray(8)
    
       Set FormPage = Item.GetInspector.ModifiedFormPages("P.2")
       Set ListBox1 = FormPage.Controls("ListBox1")
    
       For i = 0 to 7
          strArray(i) = ListBox1.List(i)
       Next
    
       Sort strArray, ListBox1
    
    End Sub
    
    Sub Sort(inpArray(), inpList)
       Dim intRet
       Dim intCompare
       Dim intLoopTimes
       Dim strTemp
    
       For intLoopTimes = 1 To UBound(inpArray)
          For intCompare = LBound(inpArray) To UBound(inpArray) - 1
             intRet = StrComp(inpArray(intCompare), _
             inpArray(intCompare + 1), vbTextCompare)
             If intRet = 1 Then
                ' String1 is greater than String2
                strTemp = inpArray(intCompare)
                inpArray(intCompare) = inpArray(intCompare + 1)
                inpArray(intCompare + 1) = strTemp
             End If
          Next
       Next
    
       inpList.Clear
    
       For intCompare = 1 To UBound(inpArray)
          inpList.AddItem inpArray(intCompare)
       Next
    
    End Sub 
  11. On the Form menu, click Run This Form.


  12. Click the P.2 tab, and click the Sort button to sort the list box.



REFERENCES

For additional information about available resources and answers to commonly asked questions about Microsoft Outlook 2000 solutions, please see the following article in the Microsoft Knowledge Base:

Q146636 OL2000: Questions About Custom Forms and Outlook Solutions

Additional query words: OutSol OutSol2000

Keywords :
Version : WINDOWS:2000
Platform : WINDOWS
Issue type : kbhowto


Last Reviewed: December 10, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.