A visitor to our Outlook Forums wanted to know how to change a subject and forward a message using rules:
I want to create a rule that I can run on a folder containing a number of messages that forwards each message to a new email address (a dropbox for a database). I need to write my own subject line, so that the database will read the subject line and then know to parse the document and extract the required information.
You can do this using either VBA or a Run a Script rule. If you using a rule to move the messages to another folder, the run a script rule is probably the best and can easily forward mail meeting specific conditions. You can run the rule as messages arrive or run it on the messages in the folder at any time using Run Rules Now.
Outlook 2003 users will receive the dreaded "something is trying to access your address book. Allow?" message. To avoid this, install ClickYes or Mapilab's Advanced Security software. Both are free. Mapilab's product will bypass the dialog (after you OK it the very first time), while ClickYes does the clicking for you.
Run a Script Rule
Press Alt+F11 to open the VB Editor and paste the following code into ThisOutlookSession. Create the rule in Outlook and select the script.
Don't forget to change the subject and email address!
Sub ChangeSubjectForward(Item As Outlook.MailItem) Item.Subject = "Test" Item.Save Set myForward = Item.Forward myForward.Recipients.Add "firstname.lastname@example.org" myForward.Send End Sub
To Delete the Sent Copy of the Message
To delete (or not save) the sent copy after it is forwarded, use myForward.DeleteAfterSubmit = True before the Send command.
Sub ChangeSubjectForward(Item As Outlook.MailItem) Set myForward = Item.Forward myForward.Recipients.Add "email@example.com" ' To BCC an address or DL, try this: 'myForward.BCC = "alias" myForward.DeleteAfterSubmit = True myForward.Send End Sub
"Change subject then forward" VBA Macro version
If you prefer a macro you can run on all messages in a folder at any time, use this code sample. This macro was put together using the script above and the code sample at Add a file number or keyword to the subject line of messages.
To use, paste into ThisOutlookSession and run, or add to a toolbar, ribbon, or QAT button.
Don't forget to change the subject and email address.
Sub ChangeSubjectThenSend() Dim myolApp As Outlook.Application Dim aItem As Object Set myolApp = CreateObject("Outlook.Application") Set mail = myolApp.ActiveExplorer.CurrentFolder For Each aItem In mail.Items aItem.Subject = "New Subject" aItem.Save Set myForward = aItem.Forward myForward.Recipients.Add "firstname.lastname@example.org" myForward.Send Next aItem End Sub
Find a code in the message body, then forward
This example combines the first script above with the macro at Use RegEx to extract text from an Outlook email message to look for a tracking code in the message body, then forward the message and include the code in the message subject.
Sub CodeSubjectForward(Item As Outlook.MailItem) ' Set reference to VB Script library ' Microsoft VBScript Regular Expressions 5.5 Dim M1 As MatchCollection Dim M As Match Set Reg1 = New RegExp With Reg1 .Pattern = "(Tracking Code\s*(\w*)\s*)" .Global = True End With If Reg1.Test(Item.Body) Then Set M1 = Reg1.Execute(Item.Body) For Each M In M1 'allows for multiple matches in the message body Item.Subject = M.SubMatches(1) & "; " & Item.Subject Next End If Item.Save Set myForward = Item.Forward myForward.Recipients.Add "email@example.com" myForward.Send End Sub
Forward messages in a quarantine mailbox
This macro forwards messages that were sent to a quarantine folder by antispam software back to the original recipient. The original subject is restored.
To use, add the macro to the ribbon or toolbar. Select the message and click the button.
Use myForward.Send to automatically send the message.
You need to set a reference to the VB Script library:
Microsoft VBScript Regular Expressions 5.5 in the VB Editor's Tools, References menu.
Sub ChangeSubjectThenForward() Dim oItem As Outlook.MailItem Dim strSendto, strSubject As String Dim myForward As MailItem ' Set reference to VB Script library ' Microsoft VBScript Regular Expressions 5.5 Dim Reg1 As RegExp Dim M1 As MatchCollection Dim Reg2 As RegExp Dim M2 As MatchCollection Dim M As Match Set oItem = ActiveExplorer.Selection.Item(1) Set myForward = ActiveExplorer.Selection.Item(1).Forward ' regex from http://slipstick.me/2k3zf ' check the original, not the forward Set Reg1 = New RegExp With Reg1 .Pattern = "(To[:](.*))" .Global = True End With If Reg1.test(oItem.Body) Then Set M1 = Reg1.Execute(oItem.Body) For Each M In M1 strSendto = M.SubMatches(1) Next End If Set Reg2 = New RegExp With Reg2 .Pattern = "(Subject[:](.*))" .Global = True End With If Reg2.test(oItem.Body) Then Set M2 = Reg2.Execute(oItem.Body) For Each M In M2 strSubject = M.SubMatches(1) Next End If myForward.Recipients.Add strSendto myForward.Subject = strSubject myForward.Display ' change to .send to automatically send End Sub
Use Advanced Security for Outlook to learn what programs are trying to access Outlook and permanently allow or deny access to the program and the next time it requests access, the action you choose will be automatically executed and Outlook Security will not annoy you with messages about trying to access e-mail addresses you have stored in Outlook. Freeware, available in English, German and Russian. Advanced Security is fully compatible with Windows 8 (32-bit and 64-bit editions) and Microsoft Office 2013 (32-bit editions only). The programs button is available on the ribbon in Outlook 2010 and Outlook 2013.
ClickYes Pro is a tuning tool for Microsoft Outlook security settings. It allows you to configure which applications can automatically send emails using Outlook and access email addresses stored in Outlook address book. ClickYes Pro runs as a background task providing a convenient icon in the taskbar notification area to manage allowed applications. It uses an encrypted storage and is highly secure and safe. Client and Server versions available. Works with Outlook 2000 - Outlook 2010.
CodeTwo Outlook WarningDoctor removes the security warnings that appear when sending mail or performing other actions recognized by Microsoft as a "risky" (for example, when you try to read some data using the Outlook or CDO API #. Especially useful for designers of macros, Visual Basic, and programmers of other scripting languages that use the object model.Outlook 2000 and up, including Outlook 2010 64bit.
First: You will need macro security set to low during testing.
To check your macro security in Outlook 2010 or 2013, go to File, Options, Trust Center and open Trust Center Settings, and change the Macro Settings. In Outlook 2007 and older, it’s at Tools, Macro Security.
After you test the macro and see that it works, you can either leave macro security set to low or sign the macro.
Open the VBA Editor by pressing Alt+F11 on your keyboard.
To put the code in a module:
- Right click on Project1 and choose Insert > Module
- Copy and paste the macro into the new module.
More information as well as screenshots are at How to use the VBA Editor
More Run a Script Samples:
- Autoaccept a Meeting Request using Rules
- Automatically Add a Category to Accepted Meetings
- Convert RTF Messages to Plain Text Format
- Create a rule to delete mail after a number of days
- Delegates, Meeting Requests, and Rules
- Forward meeting details to another address
- How to Change the Font used for Outlook's RSS Feeds
- Keep Canceled Meetings on Outlook's Calendar
- Macro to Print Outlook email attachments as they arrive
- Move messages CC'd to an address
- Outlook AutoReplies: One script, many responses
- Outlook's Rules and Alerts: Run a Script
- Process messages received on a day of the week
- Read Outlook Messages using Plain Text
- Receive a Reminder When a Message Doesn't Arrive?
- Run a script rule: Autoreply using a template
- Run a script rule: Reply to a message
- Run a Script Rule: Send a new message when a message arrives
- Run Rules Now using a Macro
- Save all incoming messages to the hard drive
- Save and Rename Outlook Email Attachments
- Save Outlook email as a PDF
- Sort messages by Sender domain
- Use a macro to copy data in Outlook email to Excel workbook
- Use a Rule to delete older messages as new ones arrive
- Use a run a script rule to mark messages read
- Use VBA to move messages with attachments