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 Dim strFilenum As String 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
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. Version 1.
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.
More Run a Script Samples:
- Autoaccept a Meeting Request using 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
- Move messages CC'd to an address
- Outlook's Rules and Alerts: Run a Script
- Process messages received on a day of the week
- Read Outlook Messages using Plain Text
- Run a script rule: Autoreply using a template
- Run a Script Rule: Send a new message when a message arrives
- Save Outlook email as a PDF
- Use a run a script rule to mark messages read