Handling Blank Fields


In the January and February 2000 installments of Outlook VBA on Demand, I showed you how to duplicate some of the Outlook Rules Wizard's features. This time, I look at something the Rules Wizard can't do directly—detect an incoming message that has a blank From or To address field. (A blank From or To field is characteristic of some junk mail.) I also show you how to delete an item and mark it with a category called Suspected Junk.

Place the code you see in Listing 1 into the Outlook VBA window's ThisOutlookSession module. This code, which the January installment introduced, prepares the Inbox so that VBA can monitor it for new items. Next, put the code you see in Listing 2, which is for the ItemAdd event handler, in ThisOutlookSession. This procedure runs when a new item enters the Inbox. Now, let's look more closely at Listing 2.

The first If statement checks to see whether the new item is an Outlook mail item. To do this, the statement tests whether the item's Class property matches the Outlook constant olMail. If you look up Class in the Help system, you'll find information about other possible constants that represent Outlook item types. Because Outlook folders can contain different types of items (e.g., a meeting request in the Inbox), your code needs to always check the Class property before accessing properties of a particular type of Outlook item.

The second If statement checks whether two of the new item's properties are equal to '''' or a blank string (i.e., whether the item's properties are empty). The use of the To property is straightforward, but notice that the code uses the SenderName property instead of a property called From. Although Outlook's folder views show a From column, no From property exists in the Outlook object model. Always check the object model (by pressing F2 in the VBA window) to make sure you're working with a valid property.

The If statement uses Or to connect the two expressions that test for blank properties. Therefore, the code on the line that follows the If statement will run if either property is blank. If you want the code to run only if both properties are blank, you use And instead of Or.

To set the category Suspected Junk on items that display no sender or recipient, the procedure uses the two statements

Item.Categories = "Suspected Junk"

You must save the item after you set the Categories property. Otherwise, the change you made to Categories won't write to the item.

Finally, you delete the updated item with the statement


The Delete method moves the item to the Deleted Items folder, rather than deleting the item permanently.

After you start Outlook with this code in your VBA module, Outlook applies the Suspected Junk label to any item arriving in the Inbox that has a blank sender or recipient field, then moves the item to the Deleted Items folder. To see the suspected junk items, you can switch to the Deleted Items folder and select the By Category view from the View menu. The Suspected Junk category lists the items.

What if you want to move such suspected junk mail messages to a Junk Mail folder, rather than putting them in Deleted Items? That question is the focus of my next column—how to write VBA code that moves items to a different folder.

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.