Windows IT Pro is the authoritative and independent resource for windows nt, windows 2000, windows 2003, windows xp. Features a collection of resources and magazines for windows IT professionals.
  
  
  Advanced Search 


August 2000

Working with Selected Items


RSS
Subscribe to Windows IT Pro | See More Visual Basic (VB) Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

Download the Code Here

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.

End of Article



Reader Comments
when we handle mail itmes in Outlook using VB, the method 'Move'

liam May 18, 2001


Is there a way to select a number of items in Tasks folder and change the past due date to current date?

Ruben April 23, 2003


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.

Sue Mosher March 03, 2004


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 Chellet March 16, 2004


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/

Sue_Mosher August 19, 2004 (Article Rating: )


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



mal4mac September 17, 2004 (Article Rating: )


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



kenarm October 21, 2004


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?

Anonymous User February 01, 2005 (Article Rating: )


You must log on before posting a comment.

If you don't have a username & password, please register now.




Top Viewed ArticlesView all articles
WinInfo Short Takes: Week of November 24, 2008

An often irreverent look at some of the week's other news, including a Vista Capable dismissal request, Zune price reductions, Morrow musings, Novell and Microsoft sitting in a tree ... two years later, Yahoo!, IE 6 on Windows Mobile, and so much more ...

Command Prompt Tricks

One reader shares his tip for setting up the command prompt to reflect a remote path. ...

PsExec

This freeware utility lets you execute processes on a remote system and redirect output to the local system. ...


Exchange Server and Outlook Whitepapers Protecting (You and) Your Data with Exchange Server 2007

StoreVault SnapManagers for Microsoft Exchange and SQL Server

Related Events The Myths & Truths of Email Management with SharePoint

Top 10 Email Security Challenges and Solutions

Mastering Exchange 2007 Server Management – May 29, 2008 (11:00 AM EST)

Check out our list of Free Email Newsletters!

Exchange Server and Outlook eBooks Spam Fighting and Email Security for the 21st Century

Understanding and Leveraging Code Signing Technologies

The Expert's Guide for Exchange 2003: Preparing for, Moving to, and Supporting Exchange Server 2003

Related Exchange Server and Outlook Resources Become a VIP member of the Windows IT Pro community!
Get it all with the VIP CD and VIP access. A $500+ value for only $279!

Subscribe to Windows IT Pro!
Solve your toughest technical problems with our experts and access 10,000 + articles online. 30% off

Monthly Online Pass - Only $5.95!
Get instant access to 10,000+ articles from Windows IT Pro Magazine!

TechNet Virtual Labs
Evaluate and test Microsoft's newest products.

Exchange & Outlook UPDATE eNewsletter
News, strategies, products, and developments in Exchange Server and Outlook messaging.

Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro Windows Dev Pro IT Job Hound ITTV
IT Library Technology Resource Directory Connected Home Windows Excavator Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 Copyright © 2008 Penton Media, Inc., All rights reserved. Terms and Use | Privacy Statement | Reprints and Licensing