Working with Selected Items

With this useful technique, you can quickly create routines that change only a few Outlook items at a time instead of an entire folder.

Sue Mosher

June 26, 2000

4 Min Read
ITPro Today logo in a gray background | ITPro Today


In the Summer 2000 issue, I showed you how to use Microsoft Outlook's ActiveWindow object to build a function that always returns the selected item, whether you're looking at an open Outlook item or an Outlook folder. This time, I show you how to work with selections that contain multiple items in the same folder. This useful technique lets you quickly create routines that change only a few Outlook items at a time instead of a whole folderful of them.

Suppose you decide, based on user requests, to alter the way certain items display in a public contacts folder. For instance, you might want to change the FileAs property so that it shows both the contact name and the company name. You don't need to change all the items, just a few strays. You need a procedure that works just with selected items.

Fortunately, Outlook 2000 includes a Selection object property for the Explorer object. (Remember that an Explorer is a window that displays a folder, and ActiveExplorer is the current Explorer window.) As you might guess, the Selection object is a collection of currently selected items in a folder. You need to remember three facts about such collections:

  • A collection can contain zero, one, or many items. Your code must be able to handle all three situations.

  • You can use a simple For Each...Next loop to process each item in the collection.

  • You can't assume that a Selection object holds only one type of Outlook item. For example, in a Contacts folder, you can select both contacts and distribution lists.

Listing 1 shows the ChangeFileAsOnSelection subroutine, which you can easily modify to handle any selection in the currently displayed folder. Callout A and Callout B mark the procedure's two user settings. The first user setting is intMaxItems, an integer that represents the largest number of items that you probably want to process at one time. If you're making a simple change to one property on each item, you can choose a fairly high number. If you plan to make many changes, update databases, or perform other complex operations, you might want to set the number lower.

The second user setting is the procedure that you want to call after you validate the size of the selection. Invoking the procedure separately makes the ChangeFileAsOnSelection subroutine reusable. You can copy the subroutine to a different code module for any situation in which you need to act on selected items. The primary changes you need to make are to the intMaxItems variable and the procedure that runs on the selection. You'll also want to change the ChangeFileAsOnSelection() name. VBA doesn't let you use the same name for duplicate procedures in different modules unless you declare the procedures with the Private keyword. For example,

Private Sub ChangeFileAsOnSelection()
The ChangeFileAsOnSelection procedure's Select Case statement uses the Count property to check the number of items in objSelection. If the user selected no items, the procedure is completed without making any changes. If the user selected more than the maximum number of items, the user sees a prompt asking whether he or she wants Outlook to process the given number of items. The user can respond Yes or No. If the user responds Yes to the prompt, or if the selection includes no more than the maximum number of items, the code runs the procedure you specify, passing the objSelection object to the procedure. In this case, the code runs the UpdateFileAs procedure, which Listing 2 shows. (Listing 2 is available exclusively in the 9032.zip file, downloadable from the Windows 2000 Magazine Web site.)

The UpdateFileAs procedure is simply a For Each...Next loop that determines whether the selected item is indeed a contact (by testing the item's Class property), then changes the FileAs property and saves the item. LastNameAndFirstName is a built-in Outlook property that returns the contact's last name and first name, separated by a comma. You can use this property instead of objItem.LastName & ", " & objItem.FirstName. CompanyName is the actual name of the property that appears as Company in Outlook forms. (Never assume that the real property name is the same as the name you see in the Field Chooser, view, or form. Press F2 to see the real property name in VBA's Object Browser window.)

I've shown you how to build a procedure that can handle any number of selected items in a folder. To use the ChangeFileAsOnSelection subroutine for a different task, simply copy the subroutine to another VBA module, give it a new name, change the intMaxItems value, and change the line that calls the procedure to which you want to pass the Selection object. I've also shown you how to set the FileAs property on contact items, so now you can clean up contact items as necessary.

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like