OL2000: How to Sort a List Box or Combo Box
ID: Q227178
|
The information in this article applies to:
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 INFORMATIONNOTE: 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.
- Open a new Task item.
- On the Tools menu, click 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 page, click New....Type TestLB for the name and click OK. Click OK again.
- On the Form menu, click View Code.
- 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")<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
- On the Form menu, click Run this Form.
- 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.
- On the Tools menu, click Forms, and then click Design a Form.
- In the Standard Forms Library, click Message, and then click Open.
- 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 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.
- On the Control Toolbox, click and drag a CommandButton onto the form.
- Right-click the command button, and then click Properties. Click the Display tab, type Sort for the Caption, and then click OK.
- On the Form menu, click View Code.
- 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
- On the Form menu, click Run This Form.
- 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
|