Windows IT Pro is the leading independent community for IT professionals deploying Microsoft Windows server and client applications and technologies.
  
  
  Advanced Search 


December 2001

Outlook 2002 Rules Wizard


RSS
Subscribe to Windows IT Pro | See More Visual Basic (VB) Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

Download the Code Here

This new version lets you link a VBA subroutine to a rule

Like any new version of a popular application, Microsoft Outlook 2002 adds some interesting capabilities to the established feature set. Topping the list for programmers are programmable views and searches, new folder properties, and a BeforeDelete event that you can cancel. But in this column, I'd like to highlight a new feature that you won't find in the object browser.

Instead, you'll find this buried treasure in the Rules Wizard. Click Tools, Rules Wizard, and create a new rule that starts from a blank rule and checks new messages as they arrive. After you set one or more conditions, look on the list of actions near the bottom for the run a script action, which Figure 1 shows. The name of the action is misleading because it doesn't run an actual VBScript script. Instead, this type of rule runs code that you create in Outlook 2002 VBA.

The ability to run VBA procedures from Rules Wizard rules is a big advance in end-user customization. To run custom code against incoming items in Outlook 98 and Outlook 97, you must use the Launcher custom action in the Rules Wizard to launch a compiled .exe program. Outlook 2000 provides the Items.ItemAdd event (which I've covered in earlier Outlook VBA on Demand columns) and Application.NewMail event, but you can't depend on either of these events to make your code run against every incoming item.

Procedures that you want to run from the Rules Wizard need to be subroutines that have one argument: a MailItem object variable. You can place these subroutines in the built-in ThisOutlookSession module or in a new module that you create. For easier management, I recommend that you create a new module, name it basMyRules, and place all subroutines for use with the Rules Wizard in that module.

A Practical Application
Let's look at a practical application of a rule that invokes an Outlook VBA script. Say you prefer to not receive or send HTML mail messages. Maybe you're worried that some as-yet-undiscovered vulnerability in Microsoft Internet Explorer (IE)—which provides the components for displaying HTML messages in Outlook—will let a virus enter your system. Or perhaps you just don't like the overhead involved in sending HTML messages, which are larger than plain-text messages, and you don't like having images that contain links to the Internet trigger your dial-up connection. You've already set the default message format to plain text.

The code in Listing 1 provides a subroutine that you can use with a rule to convert all incoming HTML messages to plain text. The BodyFormat property that the ConvertHTMLToDefault subroutine uses is new in Outlook 2002. Earlier versions of Outlook provide no simple way to convert a message to plain text.

To create the rule that uses the subroutine, follow these steps:

  1. Click Tools, Rules Wizard.
  2. In the first Rules Wizard dialog box, click New.
  3. Select Start from a blank rule, then select Check messages when they arrive; click Next.
  4. From the What condition(s) do you want to check? list, select the conditions you want to apply to this rule. If you want to convert all incoming HTML messages to plain text, click Next without setting any conditions and answer Yes when Outlook asks whether you want the rule to apply to every message you receive.
  5. From the What do you want to do with the message? list, select run a script.
  6. In the Rule description box at the bottom of the Rules Wizard dialog box, click the underlined words a script.
  7. In the Select Script dialog box, select Project1.ConvertHTMLToDefault, then click OK.
  8. Click Next and add any exceptions to the rule (e.g., mailing lists that you prefer to receive in HTML format). Click Next again.
  9. Give the rule a name, then click Finish. Click OK to close the Rules Wizard.

Whenever an HTML message arrives, the rule will convert it to plain-text format.

One problem with using the BodyFormat property to force a message to plain text is that the property doesn't handle embedded images well. If an HTML message includes an embedded image, the converted plain-text message doesn't show the image as an attachment, even though a check of the Attachments collection and the file size for the message indicate that the image is still attached. This limitation means not only that you might be missing that great photo from the company picnic but that the 100KB embedded image is taking up space in your mailbox.

To tackle both of these problems, the ConvertHTMLToDefaultWithAtts subroutine in Listing 2 saves any embedded image attachments to the file system, then deletes them from the message. The subroutine's ValidFileName() function ensures that the filename follows Windows rules for filename length and excluded characters. The ConvertHTMLToDefaultWithAtts subroutine builds a list of attached files that the subroutine removes and appends the list to the converted message as clickable hyperlinks so that you can easily open the saved attachments. You can use the techniques in the ConvertHTMLToDefaultWithAtts subroutine to build other VBA rules that handle file attachments.

End of Article



Reader Comments
I thought this article was fantastic!

I now know how to create my own scripts and in fact I have used the ConvertHTMLtoDefault script and modified it slightly to search for the word deatoo - which is where the majority of spam emails are pointing to that we get - and delete the message!

It works! No more Spam!

Brent Latham December 15, 2003


Fascinating and highly useful. I wrote a few Outlook macros, but these techniques are far better!

gabi December 29, 2003


hi:

I would like to know, more abaut the rules to permit save the attachments into the mails.

rosendo February 10, 2004


Thank you Sue, this is great! In a few seconds my Outlook was much more secure without any plugin. I'll have to get myself a book about Outlook VBA coding and try to learn more about this stuff!

fritz May 01, 2004


It adds a person multiple times in the address book if they have more than one email address instead of inserting all of that person’s email addresses within their name listing.

When you filter email to go into certain folders, it doesn’t always filter them.

Also, I receive duplicate email messages. Along with that is that as it filters, some of the duplicate email messages go into my archive folder.

Please tell me how I can remedy these problems.

Thank you,

~John~

John June 18, 2004


Your script give me an idea and good to start with vba. I encountered a problem though when I tried to modify by adding a msgbox. Now, it is not converting html to plain, I commented out the msgbox line but to no avail. I even removed and cut-n-paste again your script, no effect.

What seems to be the problem?

Allan June 23, 2004


Hi,
Hope someone might help me here. When I click on run a script the only thing I get is a blank box saying select a script with no options to program one. I am sure it is from having one of the plethora of security settings in Outlook's options set wrong. Any suggestions?

Miles July 03, 2004


You have to write the "script" first -- actually not a script, but a VBA macro, as described in this article.

Sue_Mosher August 19, 2004 (Article Rating: )


Check out this great tutorial on using Microsoft Outlook for managing your ebay & amazon sales, or use it as an Outlook programming quickstart:

http://www.321books.co.uk/ebooks/outlook-vba-tutorial.htm



mal4mac September 17, 2004 (Article Rating: )


Outstanding!

Anonymous User November 01, 2004 (Article Rating: )


 See More Comments  1   2 

You must be a registered user or online subscriber to comment on this article. Please log on before posting a comment. Are you a new visitor? Register now




Top Viewed ArticlesView all articles
Command Prompt Tricks

One reader shares his tip for setting up the command prompt to reflect a remote path. ...

WinInfo Short Takes: Week of November 23, 2009

An often irreverent look at some of the week's other news, including some post-PDC some soul searching, a Google Chrome OS announcement and a Microsoft response, Windows 7 off to a supposedly strong start, the Jonas Brothers and Xbox 360, and so much more ...

2009 Windows IT Pro Editors' Best and Community Choice Awards

Picking a favorite product from an impressive crowd of competitive offerings is never an easy task, and such was the case with our Editors' Best and Community Choice awards this year. ...


Exchange Server and Outlook Whitepapers Email Controls and Regulatory Compliance

Take Control of Your Email: Understand the Business Reasons for Email Storage Management

Related Events Deep Dive into Windows Server 2008 R2 presented by John Savill

Bail Out Your Exchange Environment

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 Introducing Left-Brain.com, the online IT bookstore
Looking for books, CDs, toolkits, eBooks? Prime your mind at Left-Brain.com

Discover Windows IT Pro eLearning Series!
Clear & detailed technical information and helpful how-to's, all in our trademark no-nonsense format

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 DevProConnections IT Job Hound
Left-Brain.com Technology Resource Directory asp.netPRO ITTV Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 © 2009 Penton Media, Inc. Terms of Use | Privacy Statement