Help with Item.sort

K

Kelley

I am pretty new to VBA programming, but with some help I have been
able to populate a ComboBox in Excel with Last Names from my Customers
folder in Outlook. Unfortunately, the list does not come in sorted
properly. I have tried to add code to correct this, but it gives an
error at run time. The code that follows is working properly with the
offending code commented out. When I remove the ' from those two
lines, it doesn't work.

Any help would be greatly appreciated. - Kelley New
_____

Option Explicit

Dim olApp As Outlook.Application
Dim olNs As Outlook.NameSpace
Dim Fldr As Outlook.MAPIFolder
Dim olCi As Outlook.ContactItem
Dim custFldr As Outlook.MAPIFolder

Private Sub UserForm_Initialize()

Set olApp = New Outlook.Application
Set olNs = olApp.GetNamespace("MAPI")
Set Fldr = olNs.Folders(1)
Set custFldr = Fldr.Folders("Customers")
'Set olCi = custFldr.Items
'olCi.Sort "[LastName]", False

For Each olCi In custFldr.Items
Me.ComboBox1.AddItem olCi.LastName
Next olCi

End Sub
 
H

Hollis D. Paul

Kelley said:
For Each olCi In custFldr.Items
Me.ComboBox1.AddItem olCi.LastName
Next olCi
To get sorted data into your list box, you want to add those last names
to an Array, sort the Array, and then set the combobox1.list = MyArray.

Hollis D. Paul [MVP - Outlook]
(e-mail address removed)
Using Virtual Access 4.52 build 277 (32-bit), Windows 2000 build 2195
http://search.support.microsoft.com/kb/c.asp?FR=0&SD=TECH&LN=EN-US

Mukilteo, WA USA
 
O

onedaywhen

No need to re-invent the wheel. Your app is in Excel so you may find
it easier to put the values in a range, perhaps on a hidden sheet, and
call the range's Sort method.

Hollis D. Paul said:
Kelley said:
For Each olCi In custFldr.Items
Me.ComboBox1.AddItem olCi.LastName
Next olCi
To get sorted data into your list box, you want to add those last names
to an Array, sort the Array, and then set the combobox1.list = MyArray.

Hollis D. Paul [MVP - Outlook]
(e-mail address removed)
Using Virtual Access 4.52 build 277 (32-bit), Windows 2000 build 2195
http://search.support.microsoft.com/kb/c.asp?FR=0&SD=TECH&LN=EN-US

Mukilteo, WA USA
 
H

Hans-Juergen Neschtschenko

Your code will work fine if you change the type of the olCi-variable.
You assign it to a collection. So make it
dim olCi as Outlook.Items
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top