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.
Is there a way to select a number of items in Tasks folder and change the past due date to current date?
Ruben, you'd use the same Selection technique, just using a different property, DueDate. You can use the Date() function to return today's date.
Hello,
Congratulations for your article ! I am currently in the need of a macro which get the text selected in an email. Can I get this with the Selection Object or is there a special way to copy the selected text in a string ?
Thanks for your help and the quality of your work,
Regards
Jerome, Outlook itself provides no such objects, although if you're using WordMail as the editor, you can use the Selection object from the WordEditor. YOu might want to take a look at the capabilities of the SafeInspector object from the Redemption library at http://www.dimastr.com/redemption/
There is a lot on moving messages between folders here:
Check out this great tutorial on using Microsoft Outlook for managing your ebay & amazon sales, or use it as an Outlook programming quickstart:
http://www.321books.co.uk/ebooks/outlook-vba-tutorial.htm
Terrific help this article.
I have utilised the methods to generate a macro that passes information from a selected contact into a word template.
I am having trouble passing the phone numbers to variables
Dim strFullName
Dim strCompanyName
Dim strFirstName
Dim strBusinessFax
Set objApp = CreateObject("Outlook.Application")
'Set objSelection = objApp.ActiveExplorer.Selection
Set objItem = objApp.ActiveExplorer.Selection
strDate = CStr(Date)
'MsgBox "Date: " & strDate
strLetter = "Bookmarks.dot"
'strMsg = objItem.Phone3
MsgBox strMsg
For Each objItem In objSel
strFullName = objItem.FullName
strCompanyName = objItem.CompanyName
strFirstName = objItem.FirstName
Set strBusinessFax = objItem.Phone3
Next
the codes a bit messy and i have to weed out the superflous bits yet but the problem i get is a message saying the property or method is not supported for the object type when it gets to the businessfax line (last line i have included here)
Any suggestions would be appreciated
Ken
A common problem when moving mail between different mail applications or when migrating from other applications to outlook is the received date is reset to the date the mail is moved.
Can you offer a suggestion on how I might adapt this macro to reset the received date to the sent date?