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 Outlookwill 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:
Click Tools, Rules Wizard.
In the first Rules Wizard dialog box, click New.
Select Start from a blank rule, then select Check messages when they arrive; click Next.
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.
From the What do you want to do with the message? list, select run a script.
In the Rule description box at the bottom of the Rules Wizard dialog box, click the underlined words a script.
In the Select Script dialog box, select Project1.ConvertHTMLToDefault, then click OK.
Click Next and add any exceptions to the rule (e.g., mailing lists that you prefer to receive in HTML format). Click Next again.
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.
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
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
Outstanding!
Anonymous User -November 01, 2004
I would LOVE to see some answers to John's comments. I have these exact same problems and they really make the whole concept of using scripts to sort/filter one's email pure wishful thinking. OL2002 = junk in this regard. Hopefully 2003 is a crapload better!
(Yes, I realize this has everything to do with M$, and nothing to do with this article. Consumer education is the key to coercing M$ to fix their broken so-called features!)
Anonymous User -November 09, 2004
How can I use it with Exchange Server?
Anonymous User -December 09, 2004
Hi,
Is there someone able to tell me how to do a script which would be ablr to import a rule in Outlook. The reason is that I would have to deploy a SPAM rule to all users in my company, or at least provide them the ability to simpy click on a button (with a script behind) that will import a .rwz file in their Outlook.
Thanks a lot,
Fred
Anonymous User -April 20, 2005
ok, but how to run a script after the message was sent ???
Free CDs Offer Fundamental Content for IT Pros Are you up to speed on the latest technologies and solutions? Don't miss out on your chance to get up to speed quickly on fundamental, in-depth information on some of the hottest topics in our library of content.
Let Your Users Reset Their Own Passwords: Free Download Try a 30 day free trial of Desktop Authority Password Self-Service – it provides an easy-to-use, robust system for allowing users to reset their own forgotten passwords or locked accounts.
Get Windows IT Pro & Mark Minasi’s Favorite Power Tools Guide Order Windows IT Pro now and get "More of Mark Minasi's Favorite Power Tools"--a in-depth guide to the most useful Windows commands --FREE with your paid order! Subscribe today, and save 58% off the cover price!
Deep Dive into VMware vSphere, eLearning Series Join John Savill to explore the major functionality capabilities of the vSphere virtualization platform, including identification of the changes from ESX 3.5.