Windows IT Pro is the leading independent community for IT professionals deploying Microsoft Windows server and client applications and technologies.
  
  
  Advanced Search 


June 2000

Working with an Item


RSS
Subscribe to Windows IT Pro | See More Outlook 2000 Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

Download the Code Here

In my Outlook VBA on Demand columns, I've worked mostly with Microsoft Outlook's application-level events. These events let you write code that reacts to new or sent items or to reminders that different items might trigger. However, you often need to apply some code against a specific Outlook item—either a selected item in the folder or an item you have open on your screen. Therefore, I want to show you how to use Outlook's ActiveWindow object to build a GetCurrentItem() function that always returns the selected item, whether you're looking at an open Outlook item or an Outlook folder.

This capability is helpful because most of the time you'll want to run your most useful Outlook code routines as toolbar macros. A macro is simply a subroutine (i.e., not a function) that has no arguments. The following qualifies as a macro:

Sub WhatsTheDate()
    MsgBox FormatDateTime(Now, vbLongDate)
End Sub

However, the following isn't a macro because it's a function:

Function WhatsTheDate() as String
    WhatsTheDate = FormatDateTime(Now, vbLongDate)
End Sub

The following is a subroutine, but it doesn't qualify as a macro because it contains an argument:

Sub WhatsTheDate(lngFormat)
    MsgBox FormatDateTime(Now, lngFormat)
End Sub

Writing two separate macros—one for folder windows (which Outlook calls an Explorer) and one for item windows (which Outlook calls an Inspector)— and keeping the two procedures updated can be a chore. The ability to write one macro that can run from a toolbar button in an Explorer or in an Inspector simplifies your life.

Fortunately, Outlook provides the ActiveWindow object. After you determine whether the window is an Explorer or an Inspector, you use a method appropriate for that type of window to get the current Outlook item, as Listing 1, page 125 shows. The Class property of ActiveWindow tells you whether the window is an Explorer or an Inspector. If the window is an Explorer, Outlook provides a Selection collection that represents the items that the user has selected in the folder. The code gets the Selection collection and checks to see that it contains at least one item. Then, the code returns the first item.

If Outlook displays an Explorer window but no item is selected in the folder—highly unlikely because, by default, Outlook always selects the item at the top of the list—GetCurrentItem() will return Nothing. If the window is an Inspector, you simply need to get the CurrentItem from the Inspector object:

Set objItem = objApp
.ActiveInspector.CurrentItem

Now that you have a versatile GetCurrentItem() function, you can use it in the ShowSenderAddress macro that Listing 2, page 125 shows. This macro uses the SenderFromAddress() function that I described in the June column to return the sender's email address. The ShowSenderAddress macro uses several statements to

  • Make sure that GetCurrentItem() returned an item and not Nothing.
  • Determine the exact kind of item that GetCurrentItem() returned by checking the Class property against the appropriate Outlook constant (olMail, in this case)
  • Proceed only if the item is a MailItem object (i.e., a mail message).

When you use GetCurrentItem() in a macro, be sure to check the Class property before you proceed. The different types of Outlook items have only a few properties in common. If you don't check the Class property, you'll probably experience errors when, for example, a user runs the macro that you designed for a MailItem object against a folder containing tasks. After you know that GetCurrentItem() has returned a valid MailItem object, the procedure displays in a message box the address that SenderFromAddress() returned.

You've learned some important differences between macros and other procedures. You also have a new GetCurrentItem() function to add to your library of Outlook VBA routines. You can use it any time you need to obtain the current item through code.

End of Article



Reader Comments
Does anyone have an idea, how to work out what the currently selected calendar is ?

So that I can hit a macro button an assign a repatitve appoinjtment to the currently SELECTED day.

Which is not the current actual day.

Thanks

Dave Crawford October 16, 2002


Dave, it is not possible to obtain any information from Outlook as to what day the user may have selected in the calendar.

Sue Mosher August 27, 2003


Listing 1 contains several undefined constants for the value of ActiveWindow.Class

Chris Kinsman January 08, 2004


It seems to work to reference "Item" as the currect email item when writing code in the "Message" form. Example:

MsgBox "Sending to: " + Item.To

Is this an acceptable technique?

Thanks

Tom Sage January 09, 2004


Great introduction and jump start to understanding the basics of Explorer v. Inspector application programming. Helped bring to light the understanding I was needing!

David K. Baker January 19, 2004


Tom, yes, that works, because Item is an intrinsic object when you're writing code behind an Outlook form (as is Application).

Sue Mosher March 03, 2004


Chris, if you're not running the code in Outlook VBA (i.e. the focus of this series of articles), you'd need to use the literal values for those constants or add a library reference, depending on the coding environment.

Sue Mosher March 03, 2004


Sue, you write excellent articles and very clever code and are obviously brilliant. But reading it makes my blood boil; that Microsoft has unleashed the twin evils of Outlook and VBA on an unsuspecting world. Just stand back and ask oneself whether any of this really makes sense or is even logical. What a waste of everyone's efforts!

Rob May 26, 2004


Sue your articles are great - you really know your stuff. I've got one question I can't seem to find the answer to anywhere: how to I get at the Notes field (the one on the standard contact form) ContactItem in VBA?
Thanks
Justin


Anonymous User January 31, 2005


Hello Sue,
Great stuff. I do have a question though: I would like a user to be prompted to select a mail item, which would then be attached to a message generated through code (the host app is ACCESS 2003). I know how to get a users input for attaching a file from a network for example, but don't know how to interrupt the code with an outlook explorer window which would allow the user to select a mail item, after which the code would resume its course. I don't need an extensive explanation (for which I'm sure you don't have the time), but would really appreciate if you could put me on the right track.

Many thanks in advance,
Stefan.

Anonymous User March 03, 2005 (Article Rating: )


You must be a registered user or online subscriber to comment on this article. Please log on before posting a comment. Are you a new visitor? Register now




Top Viewed ArticlesView all articles
Confirmed: Battery Life Issues Not Windows 7's Fault

Microsoft on Monday issued a lengthy statement about the recent Windows 7 battery controversy, echoing my assessment from earlier in the day, but backing it up with hard, cold evidence. ...

Battery Life Issues Almost Certainly Not Windows 7's Fault

While Microsoft is still investigating a notebook battery life issue that was supposedly caused by Windows 7, some interesting trends have emerged. ...

Microsoft Warns of Windows Version Expirations

Microsoft warned that this year will see three out-of-date Windows versions slip into retirement. ...


Exchange Server and Outlook Whitepapers Email Controls and Regulatory Compliance

Take Control of Your Email: Understand the Business Reasons for Email Storage Management

Related Events Top 5 Key Technologies Changing The Face of Exchange and Data Protection

Deep Dive into Windows Server 2008 R2 presented by John Savill

Bail Out Your Exchange Environment

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 Introducing Left-Brain.com, the online IT bookstore
Looking for books, CDs, toolkits, eBooks? Prime your mind at Left-Brain.com

Discover Windows IT Pro eLearning Series!
Clear & detailed technical information and helpful how-to's, all in our trademark no-nonsense format

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 DevProConnections IT Job Hound
Left-Brain.com Technology Resource Directory asp.netPRO ITTV Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 © 2010 Penton Media, Inc. Terms of Use | Privacy Statement