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:
MsgBox FormatDateTime(Now, vbLongDate)
However, the following isn't a macro because it's a function:
WhatsTheDate = FormatDateTime(Now, vbLongDate)
The following is a subroutine, but it doesn't qualify as a macro because it contains an argument:
MsgBox FormatDateTime(Now, lngFormat)
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:
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.