Microsoft KB Archive/229629

= OL98: How to Sort a List Box or Combo Box =

Article ID: 229629

Article Last Modified on 11/23/2006

-

APPLIES TO


 * Microsoft Outlook 98 Standard Edition

-



This article was previously published under Q229629





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 Partner or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Partners, please visit the following Microsoft Web site:

https://partner.microsoft.com/global/30000104

For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:

http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS

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.  Open a new Task item. On the Tools menu, point to Forms, and then click Design This Form. Click the (P.2) tab. On the Form menu, click Control Toolbox. On the Control Toolbox, click and drag a ListBox onto the form. Right-click the list box and then click Properties. On the Value tab, click New. Type TestLB for the name and click OK. Click OK again. On the Form menu, click View Code.</li>  In the Script Editor, type the following VBScript code:

Sub Item_Open

Set objFormPage = Item.GetInspector.ModifiedFormPages("P.2") Set objListBox = objFormPage.Controls("ListBox1") Set objNS = Application.GetNamespace("MAPI") ' 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

End Sub </li> On the Form menu, click Run This Form.</li> Click the P.2 page of the form.</li></ol>

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

Use a Sorting Algorithm
Sort algorithms 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 algorithm. Note that these values could also be retrieved from another source, such as an external database application.

<ol> On the Tools menu, point to Forms, and then click Design a Form.</li> In the Standard Forms Library, click Message, and then click Open.</li> Click the (P.2) tab.</li> On the Form menu, click Control Toolbox. On the Control Toolbox, click and drag a ListBox onto the form.</li> Right-click the list box and then click Properties. On the Value tab, 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.</li> On the Control Toolbox, click and drag a CommandButton onto the form.</li> Right-click the CommandButton and then click Properties. Click the Display tab, type Sort for the Caption, and then click OK.</li> On the Form menu, click View Code.</li>  In the Script Editor, type the following VBScript code:

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 </li> On the Form menu, click Run This Form.</li> Click the P.2 tab and click the Sort button to sort the list box.</li></ol>

<div class="references_section">