For Each oContact In oCntFlder.Items
If oContact.Categories = "Switch" Then
strOldFirst = oContact.FirstName
strOldLast = oContact.LastName
oContact.FirstName = strOldLast
oContact.LastName = strOldFirst
oContact.Save
The first line starts a loop that looks at each contact item in the Contacts folder. Notice that because you're working with a group of items in a folder—and not a group of items that you've selected with a query of some manner—you have to cycle through the contacts in the Items collection of that folder.
The second line checks the Categories property of the contact item to see if the contact is in the "Switch" category. Recall from my previous discussion that this is a simplified example. If you have multiple categories, you'll need to use an array to do this matching, which will make your code more complex.
If you used a built-in field instead of the category, this line might look like this:
If oContact.Mileage = "Switch" Then
Either way, if that match is made, the code proceeds to make the change.
In the third and fourth lines above, the code reads the values of the First and Last name fields from the Contact and writes those to the string objects that you created to hold them.
The fifth and sixth lines of code do the actual reversing. They take the value of the old First Name field and write it to the LastName property; then they take the value of the old Last Name field and write it to the FirstName property. By doing this, you've essentially read the old first name and written it to the last name field, and taken the old last name and written it to the first name field.
Error corrected!
Code Breakdown: Saving Data and
Basic Housekeeping
Now that you've made the change, you need to save the item with the correct data. Use the Save method of the oContact item to do that. After the record has been fixed and saved, end your If condition and use a Next statement to loop back up and repeat the process with the next contact item:
End If
Next
The final lines of code do basic housekeeping and error handling:
Set oContact = Nothing
Set oCntFlder = Nothing
Set oNS = Nothing
Exit Sub
Room:
Msgbox "Something's Wrong: " & err.description
Here the first three lines simply reset the objects you created to Nothing in order to free up the resources they may have used during this process. The Exit Sub command terminates this subprocedure.
The last little section performs basic error handling. Earlier you indicated in your code that you wanted to drop to the "Room" label in case an error was encountered. This code starts a message box that reads "Something's Wrong: ", followed by a description of the error.
While there may be other ways to solve the specific problem I posed at the start of this 10-Minute Solution, I hope this code example helps you understand some of the methods you can use to read and write to various properties within your Outlook items.
Ben M. Schorr is Director of Information Services at Damon Key Leong Kupchak Hastert, a major law firm in Honolulu, Hawaii. He has contributed to Managing Microsoft Exchange Server published by O'Reilly & Associates, is half of the "Ask the Exchange Pros" team in Exchange & Outlook magazine, and is coauthor of the upcoming Exchange 2000 Programming Bible published by Hungry Minds. You can reach him at bms@hawaiilawyer.com.