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 


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 log on before posting a comment.

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




Top Viewed ArticlesView all articles
Friday at PASS Europe 2006

Kevin talks about the closing day of the event and shares a funny Microsoft film. ...

The Memory-Optimization Hoax

Don't believe the hype. At best, RAM optimizers have no effect. At worst, they seriously degrade performance. ...

More fun TechEd 2005 Resources

Kevin points out some more TechEd resources ...


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

StoreVault SnapManagers for Microsoft Exchange and SQL Server

ETX Driving Embedded I/O

Related Events 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.
Job Openings in IT


ADS BY GOOGLE SPONSORED LINKS FEATURED LINKS

IT Connections
Dive into the new Microsoft platforms and products you implement and support with the experts from Microsoft, TechNet Magazine, Windows ITPro and industry gurus. There are 70+ sessions and interactive panels with networking opportunities.

Attention User Group Leaders...
Announcing the eNews Generator—a FREE HTML e-newsletter builder for user group leaders. Build your HTML and text e-newsletters in minutes and add Windows IT Pro & SQL Server Mag articles alongside your own message!.

Master SharePoint with 3 eLearning Seminars
Learn how to build a better SharePoint infrastructure and enable powerful collaboration with MVPs Dan Holme and Michael Noel. Register today!

Get SQL Server 2008 at WinConnections
Don’t miss Microsoft Exchange and Windows Connections conferences, the premier events for Microsoft IT Professionals in Las Vegas, November 10-13. Every attendee will receive a copy of SQL Server 2008 Standard Edition with one CAL.



Interested in Email Encryption?
Read about the advantages of identity-based encryption in this free report.

Order Your SQL Fundamentals CD Today!
Learn how to use SQL Server, understand Office integration techniques and dive into the essentials of SQL Express and Visual Basic with this free SQL Fundamentals CD.

Virtualization Congress Oct. 14-16 in London
Don't miss Virtualization Congress, the premiere EMEA conference dedicated to hardware, OS and application virtualization. Oct. 14-16.
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 Technical Resources Directory Connected Home Windows Excavator Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 Copyright © 2008 Penton Media, Inc., All rights reserved. Terms and Use | Privacy Statement | Reprints and Licensing