Subscribe to Windows IT Pro

 

Get Newsletters

  • Get the Latest News
  • Product Updates
  • Helpful Tricks
  • Productivity Tips

Subscribe Now!

January 06, 2000 10:08 AM

Flagging Incoming Items

Windows IT Pro
InstantDoc ID #7949
Rating: (2)
Downloads
7949.zip

In the January 2000 Outlook VBA on Demand, I told you about application-level events that let you write code to respond to occurrences within Microsoft Outlook. Specifically, I described how to write code to react to the presence of a new item in a folder. If you build a procedure to respond to new items in the Inbox folder, you're essentially writing a substitute or supplement for Outlook's Rules Wizard. (Remember that VBA code runs only when Outlook is running. You can't use VBA code to replace server-side rules that run when Outlook is offline.) In this installment, I show you how to duplicate the Rules Wizard's with specific words in the body condition and how to add a message flag to an incoming item.

To duplicate Rules Wizard conditions, your code checks the properties of incoming messages for specific information. Which properties might you want your code to check? An easy way to learn about message properties is to look in VBA's Object Browser.

  1. In the VBA window, select View, Object Browser, or press F2.
  2. When the Object Browser appears, as Screen 1 shows, choose Outlook in the top drop-down list (which displays All Libraries by default).
  3. In the Classes list, scroll down and select MailItem. (MailItem is the Outlook message's official object class.) In the right pane, you'll see the members of MailItem, beginning with the Actions property.
  4. If you want to see all the message properties grouped together, right-click in the Members pane and click Group Members on the pop-up menu. The Object Browser displays all the properties together, followed by the methods for the MailItem object and its events. (Methods are actions that you can perform on a MailItem.)

Table 1 maps Rules Wizard conditions to an Outlook MailItem object's properties. Notice that the relevant property isn't always obvious. For example, you might expect the Flagged for action rule to use the Actions property. Instead of the Actions property, the rule uses the FlagStatus property (if you decide to check for any message flag) or the FlagRequest property (if you decide to check for a specific flag). Until you become fluent in the language of the Outlook Object Model, you can refer to the properties' Help topics for information. To display a property's Help topic, simply select the property in the Object Browser and press F1.

Suppose you want to search an item's text for a particular word or phrase. Listing 1 shows an event handler that works on the Inbox folder to detect the ItemAdd event on the folder's Item collection. (Remember that this procedure requires the WithEvents declarations and instantiation procedures that I described in the January 2000 column.) In Listing 1, the subroutine uses the Instr() function to search the body of the item (i.e., the message's text) for the string "ASAP": If the Instr() expression finds "ASAP", the subroutine sets a message flag on the item with a due date 5 days from that moment.

Instr() is a function that lets you search inside any text (i.e., the first argument) for a word or phrase (i.e., the second argument). Instr() returns an integer value based on whether the function finds the text it's searching for. If the Instr() function fails to find the text, the result is zero. If the function finds the text, the result is the position of the search string ("ASAP", in this example) inside the text block that Instr() is searching—an integer value that is always greater than zero.

You need to know a couple of other facts about the code in Listing 1. First, the FlagRequest property isn't limited to the choices you see on the drop-down list in Outlook's Message Flag dialog box. You can flag a message with any text. Second, the expression that sets the value of the FlagDueBy property—Now() + 5—uses the special Now() function. Now() returns the current date and time. Because of the way Outlook stores dates, to add 5 days to the current date and time, you simply add 5 to Now().

Now that you know how to use Instr() to search inside text and how to set a message flag, you can build variations that also search the Subject property. In my next column, I'll continue this exploration of Inbox item processing. I'll show you how to create a rule that checks for an empty To or From field—something you might see on junkmail items, but something that the Rules Wizard can't detect with any of its conditions.

Related Content:

ARTICLE TOOLS

Comments
  • Anonymous User
    7 years ago
    Apr 25, 2005

    No Comment.

  • Anonymous User
    8 years ago
    Dec 02, 2004

    It is possible to automatically read the subject on an incoming e-mail and, based on the subject, re-format the body of the message before displaying.

  • SUE
    8 years ago
    Aug 19, 2004

    James, you should be able to use a null string in your restriction clause, too.

  • james wheeler
    11 years ago
    Aug 28, 2001




    How can i set the folder monitor to look at a public folder? I am trying to adapt your code that uses olInboxItems but am not having any luck, I want to combine that code and this code to flag items as they arrive. Can you help?

  • James Wheeler
    11 years ago
    Aug 28, 2001




    Hello,


    This is very useful to me as I am designing an e-mail handling system, However,what I need to do is refer to all those e-mails where there isn't a flag of any kind and flag them for followup, I can do the latter part but I can't refer to the e-mail having no status eg MyClause = "[FlagStatus] = ""Complete""" creates a restric clause that restricts to only e-mails with complete, what about those mails with none. Did any of that make sense? Who knows, can you help me?


    Regards, James

You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

White Papers

Get your Windows 7 deployment off to the right start by implementing PC lockdown. A locked-down environment is easier and cheaper to support since users are less likely to make unnecessary changes to the core system configuration - read more here!

Essential Guides

Is your iSCSI "lossy"? The reality is that most off-the-shelf Ethernet hardware deployed for iSCSI can lose packets, resulting in slow performance or application downtime. Learn how to assess your current iSCSI infrastructure and engineer an advanced iSCSI SAN infrastructure.

Web Seminars

What's the best way to keep your network safe from malware? In this web seminar, security expert Greg Shields suggests an alternative method to the traditional blacklisting approach that is common with anti-virus and anti-malware solutions.

eLearning Series

We bring the experts direct to you to share their real-world perspective and expertise. During each event, three sessions stream in real time, so you can learn, ask questions, and get solutions.
Upcoming event: Getting the Most with Exchange 2010 with Paul Robichaux

Subscribe to Windows IT Pro!

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