PDA

View Full Version : Outlook ComboBox Sort Code



lms
09-14-2013, 06:03 PM
The following code searches all Categories for the contacts that are in each Category, and this includes contacts from the Contact folder, and it's sub-folders, sub-sub folders, and its sub-sub-sub folders. So the Combobox named "ddlCategories" shows the list of all Categories and when I click on one of the Categories, the Combobox named "ddlContacts" shows the list of all contacts assigned to the Category I clicked on, and as to the list of Contacts that show up, each one is link to the specific contact so when I click on the Contact name, it opens up the Contact itself.
But what has happened is that the list of the Categories are not sorted on an alphabetical basis....and the Contacts that show up which are from different folders, are sorted alphabetically as to each folder it comes from, not sorted simply as to all the contacts.
So the question is, is there something to add to the code and where, so that each Combobox sorts what shows up on a full alphabetical basis? Thanks to all.


Private Sub ddlCategories_Change()
Dim objOutlook As outlook.Application
Dim objNS As outlook.NameSpace
Dim objFolder As outlook.MAPIFolder
Dim ctc As ContactItem
Dim FolderName As String
Dim fldr As folder
Dim flder As outlook.folder
Dim flderr As outlook.folder
Dim myContacts As outlook.items
Dim Category As String
Category = Me.ddlCategories.Text
Set objOutlook = CreateObject("Outlook.Application")
Set objFolder = objOutlook.Session.GetDefaultFolder(OlDefaultFolders.olFolderContacts)
Me.ddlContacts.Clear
If objFolder.Folders.Count > 0 Then
Set myContacts = objFolder.items.Restrict("[Categories] = '" & Category & "'")
If myContacts.Count > 0 Then
myContacts.Sort "[Fullname]", False
For Each ctc In myContacts
Me.ddlContacts.addItem ctc.FullName
Next
End If
For Each fldr In objFolder.Folders
Set myContacts = fldr.items.Restrict("[Categories] = '" & Category & "'")
If myContacts.Count > 0 Then
myContacts.Sort "[Fullname]", False
For Each ctc In myContacts
Me.ddlContacts.addItem ctc.FullName
Next
End If
For Each flder In fldr.Folders
Set myContacts = flder.items.Restrict("[Categories] = '" & Category & "'")
If myContacts.Count > 0 Then
myContacts.Sort "[Fullname]", False
For Each ctc In myContacts
Me.ddlContacts.addItem ctc.FullName
Next
End If
For Each flderr In flder.Folders
Set myContacts = flderr.items.Restrict("[Categories] = '" & Category & "'")
If myContacts.Count > 0 Then
myContacts.Sort "[Fullname]", False
For Each ctc In myContacts
Me.ddlContacts.addItem ctc.FullName
Next
End If
Next
Next
Next
End If
End Sub

Private Sub UserForm_Initialize()
'The loads the Outlook userform and populates the combobox of contact folders.
Dim objOutlook As outlook.Application
Dim objNS As outlook.NameSpace
Dim objFolder As outlook.MAPIFolder
Dim Category
Set objOutlook = CreateObject("Outlook.Application")
For Each Category In Application.Session.Categories
Me.ddlCategories.addItem Category
Next
End Sub

Private Sub ddlContacts_Change()
'This opens the contact form for the contact selected.
Dim objOutlook As outlook.Application
Dim objNS As outlook.NameSpace
Dim ctcItems As outlook.items
Dim ctc As ContactItem
Dim objFolder As outlook.MAPIFolder
Dim ctcFolder As outlook.MAPIFolder
Dim FolderName As String
Dim ContactName As String
Dim FoundFolder As outlook.folder
Set objOutlook = CreateObject("Outlook.Application")
Set objFolder = objOutlook.Session.GetDefaultFolder(OlDefaultFolders.olFolderContacts)
ContactName = Me.ddlContacts.Text
If objFolder.Folders.Count > 0 Then
Set ctcItems = objFolder.items.Restrict("[FullName] = '" & ContactName & "'")
If ctcItems.Count > 0 Then
Me.Hide
For Each ctc In ctcItems
ctc.Display
Next
Exit Sub
Else
For Each fldr In objFolder.Folders
Set ctcItems = fldr.items.Restrict("[FullName] = '" & ContactName & "'")
If ctcItems.Count > 0 Then
Me.Hide
For Each ctc In ctcItems
ctc.Display
Next
Exit Sub
Else
For Each flder In fldr.Folders
Set ctcItems = flder.items.Restrict("[FullName] = '" & ContactName & "'")
If ctcItems.Count > 0 Then
Me.Hide
For Each ctc In ctcItems
ctc.Display
Next
Exit Sub
Else
For Each flderr In flder.Folders
Set ctcItems = flderr.items.Restrict("[FullName] = '" & ContactName & "'")
If ctcItems.Count > 0 Then
Me.Hide
For Each ctc In ctcItems
ctc.Display
Next
Exit Sub
End If
Next
End If
Next
End If
Next
End If
End If
End Sub

Private Function FullFolderName(ByVal FolderName As String) As outlook.folder

End Function

mrojas
09-15-2013, 06:45 PM
It seems to me that you'll need to create an array to hold the contact inforamation as you loop through each folder. Once you've populated the array, traverse it adding each element to the drop-down list.

mrojas
09-15-2013, 06:47 PM
Here's a link on how to sort an array:
http://www.access-programmers.co.uk/forums/showthread.php?p=975589

lms
09-15-2013, 06:55 PM
Thanks much. I appreciate the thoughts but I don't have the background of figuring out to do it.....is there any possible way you can add to the code I showed what to add to or change it and I can give it a try? I would so appreciate it as I just don't have that experience. Thanks much!!

mrojas
09-15-2013, 07:08 PM
I'll be glad to look at your code a bit closer, but please go back to it and add as many comments as possible; this will make it easier for anyone, I included, reading your code to know exactly what you're doing or intending to do.

lms
09-15-2013, 07:23 PM
I so thank you for the help!!! The full code from the beginnng identifies all the Categories that are in Outlook....I use Outlook 2007...


And then, per each Outlook Contact folder and then each subfolder, subsubfolder and subsubsubfolder, it restricts the contacts that come with the Categories.....and then toward the end, it sets it up as the links to the contacts themselves..


And so when I run the code, it shows the Categories in the Combobox named "ddlCategories" and then when I click on one of the Categories, the Contacts related to the Category I click, shows up in the Combobox named "ddlContacts" but since it comes from different folders, it does not sort all alphebeticaly as if one folder....and for whatever reason the ddlCategories" list is also not sorted on an alphabetical way...

lms
09-15-2013, 07:34 PM
And I am happy to email to anyone the full code in a Notepad or Word document so you can copy and past in run it....

All you need to do it create a Useform and two Comboboxes named what I named, and then past the code to it, and you will see what happens if you have Categories and folders, subfolders etc for contacts.

lms
09-16-2013, 12:57 PM
Any possible update today? Would be excited to get this done!!

mrojas
09-16-2013, 03:04 PM
My email address is Rojas at Astound dot net.