• Outlook User
  • Exchange Admin
  • Office 365
  • Outlook Developer
  • Outlook.com
  • Outlook Mac
  • Outlook & iCloud
    • Common Problems
    • Outlook BCM
    • Utilities & Addins
    • Video Tutorials
    • EMO Archives
    • Outlook Updates
    • Outlook Apps
    • Forums

Run a Script Rule: Change Subject then Forward Message

Slipstick Systems

› Outlook › Rules, Filters & Views › Run a Script Rule: Change Subject then Forward Message

Last reviewed on December 1, 2018     324 Comments

A security update disabled the Run a script option in Outlook 2013 and 2016's rules wizard. See Run-a-Script Rules Missing in Outlook for more information and the registry key to fix restore it.

April 16, 2012 by Diane Poremsky 324 Comments

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.

Change subject and forward rule

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 "alias@domain.com"

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 "alias@domain.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 "alias@domain.com"
    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 "alias@domain.com"
 
myForward.Send
 
End Sub

Forward Attachment & Change Subject

This version of the run a script macro gets the attachment name and puts it in the subject field. If there are multiple attachments, it exits the macro after the first matching attachment (and Excel file in this sample).

Sub ChangeSubjectForwardAttachment(Item As Outlook.MailItem)
Dim oAtt As Attachment
strAtt = ""
For Each oAtt In Item.Attachments
Debug.Print oAtt.FileName

If Right(oAtt.FileName, 5) = ".xlsx" Then

  Set myforward = Item.Forward
   myforward.Recipients.Add "alias@domain.com"
   myforward.Subject = oAtt.FileName
   myforward.Display 'Send
   Exit Sub

End If
Next oAtt

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

 

Add the sender name to a read receipt

Michal wanted to add the sender's name to a read receipt subject. Because it's a Report item and not a Message, you need to tweak the script a bit. The Reports also don't have a sender name, but you can use Regular Expressions (regex) to grab the name from the message body.

Unlike the scripts above, this script is using late-binding with the regex library. This makes the macro more portable, as you don't need to set the reference to the Regex library. If you are using multiple macros with regex, it's generally better to use early binding.

The Rule condition will be "uses the Report form" (choose Reports from Application Forms in the dialog). Note: if run script is not an option in Actions, see "Run-a-Script Rules Missing in Outlook".

read reciept report rule

Sub AddSender(Item As Outlook.ReportItem)
 
Dim Reg1 As Object 'RegExp
Dim Matches As Object 'MatchCollection
Dim Match As Object 'Match
Dim strSender As String

Set Reg1 = CreateObject("VBScript.RegExp")

Reg1.Pattern = "To[:]\s*((.*))\r"

Set Matches = Reg1.Execute(Item.Body)

For Each Match In Matches
  strSender = Match.SubMatches(1)
Next

 Item.Subject = Item.Subject & " - " &  strSender
 Item.Save
    
End Sub

Testing Run a Script Macros

Testing run a script macros tends to be a PITA, since they only run when new messages arrive or when you run the rule manually. Sending new messages takes time and using Run Rules Now with a bad script or the wrong conditions can really screw up your day.

Tip: If you need to test the rule conditions, you can't avoid using Run Rules Now but you can minimize risk if you copy messages to a new folder and run it on messages in that folder.

When you just need to test the script, you can use a simple "stub" macro that calls the script, running it on the selected message.

Sub RunScript()
Dim objApp As Outlook.Application
Dim objItem As Object ' MailItem
Set objApp = Application
Set objItem = objApp.ActiveExplorer.Selection.Item(1)

'macro name you want to run goes here
YourMacroName objItem

End Sub

 

Tools

ClickYes Pro

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

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.

Outlook Security Manager

Developers can use this to avoid the security prompts in Outlook.

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:

  1. Right click on Project1 and choose Insert > Module
  2. Copy and paste the macro into the new module.

More information as well as screenshots are at How to use the VBA Editor

More Information

More Run a Script Samples:

  • Autoaccept a Meeting Request using Rules
  • Automatically Add a Category to Accepted Meetings
  • Blocking Mail From New Top-Level Domains
  • Convert RTF Messages to Plain Text Format
  • Create a rule to delete mail after a number of days
  • Create a Task from an Email using a Rule
  • Create an Outlook Appointment from a Message
  • Create Appointment From Email Automatically
  • Delegates, Meeting Requests, and Rules
  • Delete attachments from messages
  • Forward meeting details to another address
  • How to Change the Font used for Outlook's RSS Feeds
  • How to Process Mail After Business Hours
  • Keep Canceled Meetings on Outlook's Calendar
  • Macro to Print Outlook email attachments as they arrive
  • Move messages CC'd to an address
  • Open All Hyperlinks in an Outlook Email Message
  • 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
  • Run-a-Script Rules Missing in Outlook
  • Save all incoming messages to the hard drive
  • Save and Rename Outlook Email Attachments
  • Save Attachments to the Hard Drive
  • Save Outlook Email as a PDF
  • Sort messages by Sender domain
  • Talking Reminders
  • To create a rule with wildcards
  • Use a Macro to Copy Data in an Email to Excel
  • 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

Run a Script Rule: Change Subject then Forward Message was last modified: December 1st, 2018 by Diane Poremsky

Related Posts:

  • Run a Script Rule: Send a New Message when a Message Arrives
  • A simple run a rule script marks Outlook messages read when the messag
    Use a run a script rule to mark messages read
  • Run a script rule: Autoreply using a template
  • Run a script rule: Reply to a message

About Diane Poremsky

A Microsoft Outlook Most Valuable Professional (MVP) since 1999, Diane is the author of several books, including Outlook 2013 Absolute Beginners Book. She also created video training CDs and online training classes for Microsoft Outlook. You can find her helping people online in Outlook Forums as well as in the Microsoft Answers and TechNet forums.

324
Leave a Reply

2500
Photo and Image Files
 
 
 
Audio and Video Files
 
 
 
Other File Types
 
 
 
2500
Photo and Image Files
 
 
 
Audio and Video Files
 
 
 
Other File Types
 
 
 

  Subscribe  
newest oldest most voted
Notify of
David
David

I want to just change the subject line of messages in my mailbox. I've adapted your script and that works OK for the subject shown in the reading pane, but not the subject shown in the message list. This holds even after switching to a different folder and back, or even restarting Outlook. I've looked through the object model (https://docs.microsoft.com/en-us/office/vba/api/outlook.mailitem), but don't see any "rebuild the inbox table of subjects" method.

Vote Up00Vote Down Reply
February 12, 2019 8:39 pm
Diane Poremsky
Diane Poremsky

The problem is most likely that you are using the conversation view - it picks the conversation up from the header, not from the subject field.

Change the Subject of an Incoming Message

Vote Up00Vote Down Reply
February 12, 2019 11:01 pm
kelli stark
kelli stark

How can I add a comment on the subject? just into the email I am auto-forward and keeping the original subject in my inbox. you can check the link if you have problems related to the Microsoft support service.

Vote Up00Vote Down Reply
February 28, 2018 1:43 am
Diane Poremsky
Diane Poremsky

if you want to edit the subject of the forward, do it like this:
Sub ChangeSubjectForward(Item As Outlook.MailItem)
Set myForward = Item.Forward
myforward.Subject = "Test"
myForward.Recipients.Add "alias@domain.com"

myForward.Send

End Sub

Vote Up00Vote Down Reply
February 28, 2018 5:36 pm
Amr
Amr

Hi,

Is there a way to change the subject and forward the email with no FW in the subject line. Also, could it be forwarded for just emails with a specific words in the subject line rather, or from a specific email, please?

Cheers,

A.E.

Vote Up00Vote Down Reply
December 10, 2017 4:21 am
Elizabeth.vangorkom
Elizabeth.vangorkom

Hi Diane;

I hope you can help me; I have used the script above as you described (it is all I need to be honest):

"Sub ChangeSubjectForward(Item As Outlook.MailItem)
Item.Subject = "8980 - RCB"
Item.Save

Set myForward = Item.Forward
myForward.Recipients.Add "xxxx@xxx.com"

myForward.Send

End Sub

but what I would like to do is to only run this on a specific sub-folder and then delete the email from said sub-folder after the email has been sent.

Many thanks

Vote Up00Vote Down Reply
September 27, 2017 10:40 am
Diane Poremsky
Diane Poremsky

That is set up as rule... so you can run the rule manually on a folder, but that is not automatic... if you need automatic, you can use an Itemadd macro to watch the folder and do the forward. See https://www.slipstick.com/developer/itemadd-macro/ for a sample.

Vote Up00Vote Down Reply
October 8, 2017 6:03 pm
Clint
Clint

Hi Diane, I have found your site very helpful however I am lost between a few of your scripts.I'm am trying to create a macro that Checks the subject line for key words "Unclassified" "Private" "Restricted" and if these exist or don't exist prompt a Userform with a radio button to add "Unclassified" to the existing Subject or provide a check box to edit the Subject line prior to sending. Can you help? Thanks Clint

Vote Up00Vote Down Reply
September 12, 2017 1:05 pm
Diane Poremsky
Diane Poremsky

i don't have any that complicated posted on the site - will see if i have one in my cache. The macros at https://www.slipstick.com/developer/code-samples/add-secure-message-subject-sending/ add 'secure' - i don't thnk any of the samples check for the keyword first, but that isn't difficult to do using instr function.

Vote Up00Vote Down Reply
September 14, 2017 12:01 am
Stephanie
Stephanie

Hi Diane,
I am trying to add a time delay to the script below, any suggestions?

Sub WCBClearanceLtrEmail(Item As Outlook.MailItem)
Set myForward = Item.Forward
'forward to a distribution list'
myForward.Recipients.Add "WCB"
'change subject'
myForward.Subject = "WCB Clearance Denied List"
'adding font information for signature block and body of message'
Arial75 = ""
Arial10 = ""
Arial11 = ""
EndFont = "
"
'my message info'
BodyText = Arial11 & "The contractors on the attached list do not have proper WCB coverage or are not in good standing with WCB.
Please ensure they are not on site or being utilized by your location in any way.

Thank you and have a safe day,

" & EndFont
myForward.HTMLBody = BodyText & BodySig & Disclaimer
myForward.Send
End Sub

Vote Up00Vote Down Reply
August 10, 2017 10:32 am
Diane Poremsky
Diane Poremsky

You can set a deferred delivery time using code - samples are here: https://www.slipstick.com/developer/code-samples/delay-sending-messages-specific-times/ - those are itemsend macros, so you'll only want to swipe the revelant code to add to your macro - this should delay it 6 hours or you can set a specific time (which the examples on that pages do).
SendAt = Now + .25
myForward.DeferredDeliveryTime = SendAt

Vote Up00Vote Down Reply
August 10, 2017 11:16 am
Stephanie
Stephanie

Thank you.

Vote Up00Vote Down Reply
August 11, 2017 10:44 am
Oscar Jara
Oscar Jara

Also, how can I add a comment on the subject just into the email I am autoforward and keeping the original subject on my inbox

Vote Up00Vote Down Reply
August 8, 2017 3:12 pm
Diane Poremsky
Diane Poremsky

Yes, you'd make the change on myforward, not on the item.

Set myForward = Item.Forward
myForward.subject "New Subject"

if you want to include the old subject, use
myForward.subject "New Subject " & item.subject

Vote Up00Vote Down Reply
August 10, 2017 1:11 pm
Oscar Jara
Oscar Jara

Even with "myForward.DeleteAfterSubmit = True", I am still having an email on my send folder.

Vote Up00Vote Down Reply
August 8, 2017 3:10 pm
Diane Poremsky
Diane Poremsky

This is on your gmail account? Gmail always saves a copy of sent items - it's independant of outlook. In fact, auto account setup in outlook 2013 and up set gmail imap accounts to not save sent items to avoid duplicated sent messages.

Vote Up00Vote Down Reply
August 10, 2017 11:18 am

Visit Slipstick Forums.
What's New at Slipstick.com

Latest EMO: Vol. 24 Issue 3

Support Services

Do you need help setting up Outlook, moving your email to a new computer, migrating or configuring Office 365, or just need some one-on-one assistance?

Subscribe to Exchange Messaging Outlook






Our Sponsors

  • Popular
  • Latest
  • Week Month All
  • Adjusting Outlook's Zoom Setting in Email
  • The Signature or Stationery and Fonts button doesn't work
  • Security Certificate Warning in Microsoft Outlook
  • This operation has been cancelled due to restrictions
  • How to Remove the Primary Account from Outlook
  • Two Copies of Sent Messages in Outlook
  • iCloud error: Outlook isn't configured to have a default profile
  • Outlook's Rules and Alerts: Run a Script
  • Outlook is Not Recognized as the Default Email Client
  • To Cc or Bcc a Meeting Request
  • Outlook.com: Manage Subscriptions
  • Group By Views don’t work in To-Do List
  • Category shortcuts don’t work
  • How to disable the Group By view in Outlook
  • Adjusting Outlook's Zoom Setting in Email
  • Change the Subject of an Incoming Message
  • Creating Signatures in Outlook
  • Scheduling a Recurring Message
  • OneNote is missing from Office 365 / 2019
  • Create Rules using PowerShell
Ajax spinner

Newest VBA Samples

Adjusting Outlook's Zoom Setting in Email

Move email items based on a list of email addresses

Remove prefix from Gmail meeting invitations

How to hide LinkedIn, Facebook, Google and other extra contact folders in Outlook.com

Use VBA to create a Mail Merge from Excel

Open multiple Outlook windows when Outlook starts

Set most frequently used Appointment Time Zones

How to change the From field on incoming messages

VBA: File messages by client code

Update Contact Area Codes

Recent Bugs List

Microsoft keeps a running list of issues affecting recently released updates at Fixes or workarounds for recent issues in Outlook for Windows.

Windows 10 Issues

  • iCloud, Outlook 2016, and Windows 10
  • Better Outlook Reminders?
  • Coming Soon to Windows 10: Office 365 Search
  • Outlook Links Won’t Open In Windows 10
  • BCM Errors after Upgrading to Windows 10
  • Outlook can’t send mail in Windows 10: error Ox800CCC13
  • Missing Outlook data files after upgrading Windows?

Outlook 2016 Top Issues

  • The Windows Store Outlook App
  • Emails are not shown in the People Pane (Fixed)
  • Calendars aren’t printing in color
  • The Signature or Stationery and Fonts button doesn’t work
  • Outlook’s New Account Setup Wizard
  • BCM Errors after October 2017 Outlook Update
  • Excel Files Won’t Display in Reading Pane
  • Outlook 2016: No BCM
  • Exchange Account Set-up Missing in Outlook 2016

VBA Basics

How to use the VBA Editor

Work with open item or selected item

Working with All Items in a Folder or Selected Items

VBA and non-default Outlook Folders

Backup and save your Outlook VBA macros

Get text using Left, Right, Mid, Len, InStr

Using Arrays in Outlook macros

Use RegEx to extract message text

Paste clipboard contents

Windows Folder Picker

Custom Forms

Designing Microsoft Outlook Forms

Set a custom form as default

Developer Resources

Developer Resources

Developer Tools

Outlook-tips.net Samples

VBOffice.net samples

OutlookCode.com

SlovakTech.com

Outlook MVP David Lee

MSDN Outlook Dev Forum

Repair PST

Convert an OST to PST

Repair damaged PST file

Repair large PST File

Remove password from PST

Merge Two Data Files

Sync & Share Outlook Data

  • Share Calendar & Contacts
  • Synchronize two computers
  • Sync Calendar and Contacts Using Outlook.com
  • Sync Outlook & Android Devices
  • Sync Google Calendar with Outlook
  • Access Folders in Other Users Mailboxes
  • “Live” Group Calendar Tools

Convert to / from Outlook

  • Converting Messages and Calendar or
    Address books
  • Moving Outlook to a New Computer
  • Moving Outlook 2010 to a new Windows computer
  • Moving from Outlook Express to Outlook

Recover Deleted Items

  • Recover deleted messages from .pst files
  • Are Deleted Items gone forever in Outlook?

Outlook 2013 Absolute Beginner's Guide

Diane Poremsky [Outlook MVP]

Make a donation

Calendar Tools

Schedule Management

Calendar Printing Tools

Calendar Reminder Tools

Calendar Dates & Data

Time and Billing Tools

Meeting Productivity Tools

Duplicate Remover Tools

Mail Tools

Sending and Retrieval Tools

Mass Mail Tools

Compose Tools

Duplicate Remover Tools

Mail Tools for Outlook

Online Services

Productivity

Productivity Tools

Automatic Message Processing Tools

Special Function Automatic Processing Tools

Housekeeping and Message Management

Task Tools

Project and Business Management Tools

Choosing the Folder to Save a Sent Message In

Run Rules on messages after reading

Help & Suggestions

Outlook Suggestion Box (UserVoice)

Slipstick Support Services

Contact Tools

Data Entry and Updating

Duplicate Checkers

Phone Number Updates

Contact Management Tools

Sync & Share

Share Calendar & Contacts

Synchronize two machines

Sharing Calendar and Contacts over the Internet

More Tools and Utilities for Sharing Outlook Data

Access Folders in Other Users Mailboxes

View Shared Subfolders in an Exchange Mailbox

"Live" Group Calendar Tools

Home | Outlook User | Exchange Administrator | Office 365 | Outlook.com | Outlook Developer
Outlook for Mac | Outlook BCM | Common Problems | Utilities & Addins | Tutorials
Outlook & iCloud Issues | Outlook Apps
EMO Archives | About Slipstick | Advertise | Slipstick Forums
Submit New or Updated Outlook and Exchange Server Utilities

Send comments using our Feedback page
Copyright © 2019 Slipstick Systems. All rights reserved.
Slipstick Systems is not affiliated with Microsoft Corporation.

You are going to send email to

Move Comment