Become a Columnist Microsoft Exchange Site Microsoft Support SiteMSDN Exchange Site

   

h
 

Sort Outlook address book by last name

This code will sort the Outlook address book by last name. It will also reset the order to first name if needed. The complete instructions are in a KB article at http://www.microsoft.com/kb/articles/Q165/1/39.htm but there are some problems with the code there.

The Code

Sub CommandButton1_Click()
'This will sort ONLY contacts in CURRENT folder by LastName, FirstName

Set CurFolder=Application.ActiveExplorer.CurrentFolder
If CurFolder.DefaultItemType=2 Then

MsgBox "This process may take some time. You will be notified when" & _
" complete.",,"Contact Tools Message"
Set MyItems=CurFolder.Items
For i = 1 to MyItems.Count
Set MyItem=MyItems.Item(i)

If Trim(MyItem.LastNameandFirstName)<>"" Then
MyItem.Subject=MyItem.LastNameandFirstName
MyItem.Save
End If

Next

MsgBox "Done sorting Outlook Address Book contacts by Last" & _
" Name!",64,"Contact Tools Message"

Else

MsgBox "The current folder is not a Contact folder.",64,"Contact" & _
" Tools Message"

End If

End Sub

Sub CommandButton2_Click()

'This will reset contacts in CURRENT folder to FirstName LastName

Set CurFolder=Application.ActiveExplorer.CurrentFolder

If CurFolder.DefaultItemType=2 Then

MsgBox "This process may take some time. You will be notified when" & _
" complete.",,"Contact Tools Message"
Set MyItems=CurFolder.Items

For i = 1 to MyItems.Count
Set MyItem=MyItems.Item(i)

If Trim(MyItem.FullName)<>"" Then
MyItem.Subject=MyItem.FullName
MyItem.Save
End If

Next

MsgBox "Done sorting Outlook Address Book contacts by First" & _
" Name!",64,"Contact Tools Message"

Else

MsgBox "The current folder is not a Contact folder.",64,"Contact" & _
" Tools Message"

End If

End Sub


Disclaimer: Your use of the information contained in these pages is at your sole risk. All information on these pages is provided "as is", without any warranty, whether express or implied, of its accuracy, completeness, fitness for a particular purpose, title or non-infringement, and none of the third-party products or information mentioned in the work are authored, recommended, supported or guaranteed by Stephen Bryant or Pro Exchange. OutlookExchange.Com, Stephen Bryant and Pro Exchange shall not be liable for any damages you may sustain by using this information, whether direct, indirect, special, incidental or consequential, even if it has been advised of the possibility of such damages.

Copyright Stephen Bryant 2008