Windows IT Pro is the authoritative and independent resource for windows nt, windows 2000, windows 2003, windows xp. Features a collection of resources and magazines for windows IT professionals.
  
  
  Advanced Search 


March 2000

Handling Blank Fields


RSS
Subscribe to Windows IT Pro | See More Exchange Server and Outlook Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

Download the Code Here

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.

End of Article



Reader Comments

You must log on before posting a comment.

If you don't have a username & password, please register now.




Top Viewed ArticlesView all articles
10 Reasons to Deploy Windows Vista

The decision to upgrade your XP systems to Vista is simple when you consider features such as easier backup, a great desktop search, and vastly improved security options. ...

10 Reasons Not to Deploy Windows Vista

The decision to upgrade to Vista has to make business sense, but many companies find the costs in training and application compatibility problems outweigh any benefits Vista brings. ...

WinInfo Short Takes: CES 2009 Special Edition

An often irreverent look at some of the week's other CES 2009 news, including covering the Vegas spectacle from the comfort of my own home, Windows 7 public beta, a weird Microsoft song application, Palm Pre, pending Microsoft mobile moves, and much more ...


Exchange Server and Outlook Whitepapers Protecting (You and) Your Data with Exchange Server 2007

StoreVault SnapManagers for Microsoft Exchange and SQL Server

Related Events Virtualization Forum: Optimizing Storage, Networks, Desktops, and Security

Cloud Computing Forum: Integrating Software, Server and Storage as a Service into Your Enterprise IT Delivery Model

Virtualization Forum: Optimizing Storage, Networks, Desktops, and Security

Check out our list of Free Email Newsletters!

Exchange Server and Outlook eBooks Spam Fighting and Email Security for the 21st Century

Understanding and Leveraging Code Signing Technologies

The Expert's Guide for Exchange 2003: Preparing for, Moving to, and Supporting Exchange Server 2003

Related Exchange Server and Outlook Resources Become a VIP member of the Windows IT Pro community!
Get it all with the VIP CD and VIP access. A $500+ value for only $279!

Subscribe to Windows IT Pro!
Solve your toughest technical problems with our experts and access 10,000 + articles online. 30% off

Monthly Online Pass - Only $5.95!
Get instant access to 10,000+ articles from Windows IT Pro Magazine!

TechNet Virtual Labs
Evaluate and test Microsoft's newest products.

Exchange & Outlook UPDATE eNewsletter
News, strategies, products, and developments in Exchange Server and Outlook messaging.

Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro Windows Dev Pro IT Job Hound ITTV
IT Library Technology Resource Directory Connected Home Windows Excavator Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 Copyright © 2009 Penton Media, Inc., All rights reserved. Terms and Use | Privacy Statement | Reprints and Licensing