Code Sample Pages

Resend This Message -- automation code -- added 2/25/2008

Event Code to move emails -- NEW! added 4/27/2008


Code Samples


Etiquette Check Code

I am a big believer in netiquette, that is why I use the ItemSend event of the Outlook Application object to monitor my outgoing emails for appropriate subject line, number of attachments, etc. Just replace "part of your signature line" in the code with a line from your signature that would only appear if it was in the email. For example, your phone number or full name.




Redirect E-Mail message

This macro, added to the email toolbar and/or the Explorer toolbar, will forward the active/selected mail item to the recipient of your choice. The original sender of the mail and the newly selected recipient are placed in the "To:" field. Replies are directed to the newly selected recipient (via Options button, 'Send Replies To') and everyone else from the original email is CC'd (so they can ignore). You can also disable 'Reply To All' so the original sender or the new recipient can't send any more irrelevant emails to you.


Download the code here.

This code is adapted from code found at Aaron Lerch's blog and also some excellent code at Scott Hanselman's blog.




Creating Followup Tasks

This macro, attached to a toolbar button in a mail message, creates a followup task on the email and attaches the original msg to the task. You can also run it from the explorer window, just select ONE email message, then run the code. This is based on code found in Sue Mosher's book (check the Programmer's Library) and here as well. I recommend you drag this macro to one of the Explorer toolbars, and also open a mail message and add it to one of the message toolbars, so you can use it in either circumstance.




Lunch Special

A routine (that works in Excel as well) that pops up a MsgBox with a random msg. In this case, it a fun app that helps me choose what to eat for lunch. I can see many other uses for this code. I use the same code in the Application_Startup event to pop up a random msg when Outlook starts.


I attach this code to a toolbar button to run it on demand. If you have a different number of options, simply change the "5" in the code to whatever number you want. For example, if you had 10 options, the code would read X = Int((10 - 1) * Rnd + 1). Then you would need to add additional "Case" statements as appropriate. Hopefully your meal choices are better than mine.




Create Excel Workbook (From Outlook)





Automate Outlook, open Excel and run Macro on .xls file attachment

Oftentimes I find it extremely useful to completely automate some macro process; to make a procedure completely hands-off. I can leave Outlook running 24/7 and then use a procedure like the one below to run an Excel macro from Outlook.

This code runs as an event, checking each incoming email for certain characteristics. If an email matches the profile, it opens the attachment (by saving it first), runs a pre-written macro, then cleans up and moves the now-processed email out of the Inbox to another folder.

You could easily adapt this code to many other purposes, for example, set up an automated file request system in your office. Users would email you with specific words in the subject, and, in response, your code could run a macro or attach a specific file to a blank email and send it back to them.

First thing you will need to do is declare a new class by putting this line at the top of your ThisOutlookSession module:

Private WithEvents Items As Outlook.Items

ThisOutlookSession is a built-in class module, so I usually put all my event/class code here to avoid confusion.

Then, in the Application_Startup() event, place the following code to instantiate the Items class:

If you don't already have a Startup event, simply add Private Sub Application_Startup() to the top of the above code, and End Sub to the bottom. This will tell Outlook that there will be some events associated with the Items collection.

Once you paste in the code above, you will be able to choose the Items class from the dropdown boxes at the top of the VBE code window. The dropdown box on the right will show you the possible events you can monitor from the Items collection.

Outlook VBE

Finally, here is where the real work gets done. The code monitors the default Inbox for any new items added. If they are mail messages, it checks the sender, subject and number of attachments. We have already pre-determined where the email will come from, the subject line and how many attachments. The code will open the attached workbook, run a pre-written macro on it, then move the email to our storage folder, all without any user input needed.

And don't forget to check out the Excel code samples while you are here.

LAST UPDATED: April 27, 2008