Subscribe to Windows IT Pro

 

Get Newsletters

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

Subscribe Now!

February 09, 2000 11:58 AM

Handling Blank Fields

Windows IT Pro
InstantDoc ID #8141
Rating: (0)
Downloads
8141.zip

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"
Item.Save

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

Item.Delete

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.

Related Content:

ARTICLE TOOLS

Comments
    There are no comments to display. Be the first one!
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.