OL98: How to Sort a List Box or Combo BoxID: Q229629
|
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.
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/refguide/
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
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
For more information about creating solutions with Microsoft Outlook 98,
please see the following articles in the Microsoft Knowledge Base:
Q180826 OL98: Resources for Custom Forms and Programming
Q182349 OL98: Questions About Custom Forms and Outlook Solutions
Additional query words: OutSol OutSol98 98
Keywords : kbdta OffVBS
Version : WINDOWS:
Platform : WINDOWS
Issue type : kbhowto
Last Reviewed: June 21, 1999