Subscribe to Windows IT Pro
May 17, 2000 10:28 AM

Working with an Item

Windows IT Pro
InstantDoc ID #8783
Rating: (1)
Downloads
8783.zip

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.

Related Content:

ARTICLE TOOLS

Comments
  • Anonymous User
    7 years ago
    Mar 03, 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
    7 years ago
    Jan 31, 2005

    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

  • Robert
    8 years ago
    May 26, 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!

  • Sue Mosher
    8 years ago
    Mar 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
    8 years ago
    Mar 03, 2004

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

You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.