I put this solution together in response to a comment on my post How to Avoid Blank Subject Lines in Outlook 2003-2007. The comment in question came from a gentleman named Robert who asked
Do you have a script that would remove RE and FWD from all messages? One of the folks on the legal team here does not want those items in his mail.
I didn’t have a script that does this, so I put this solution together for Robert. The script works by checking the subject line of inbound messages for “FW:”, “Fwd:”, and “RE:”, and the subject line of outbound messages for “FW:” and “RE:”. If it finds any of those values, then the script strips them out. As a bonus I’ve added the capability to turn checking on/off for both sending and receiving with a simple toggle. By default the script will check both sent and received messages. The toggle gives the end user the ability to turn checking off for one or both as they see fit.
Requirements.
This solution should work in Outlook 2007 and later. Outlook has to be open and running for scripts to work.
Instructions and Code.
The code comes in two parts.
Part 1
Follow these instructions to add part 1 of the code.
- Start Outlook
- Press ALT+F11 to open the VB editor
- If not already expanded, expand Microsoft Office Outlook Objects
- Right-click on Class Modules, select Insert > Class Module
- In the Properties panel click on Name and enter clsSendAndReceive
- Copy the code from the Code Snippet box and paste it into the right-hand pane of Outlook’s VB Editor window
- Click the diskette icon on the toolbar to save the changes
Const CLASS_NAME = "SendAndReceive"
Private WithEvents olkApp As Outlook.Application
Private bolSend As Boolean, bolReceive As Boolean
Private Sub Class_Initialize()
bolSend = True
bolReceive = True
Set olkApp = Outlook.Application
End Sub
Private Sub Class_Terminate()
Set olkApp = Nothing
End Sub
Private Sub olkApp_ItemSend(ByVal Item As Object, Cancel As Boolean)
If (Left(Item.Subject, 4) = "FW: ") Or (Left(Item.Subject, 4) = "RE:") Then
Item.Subject = Mid(Item.Subject, 5)
Item.Save
Else
If Left(Item.Subject, 5) = "Fwd: " Then
Item.Subject = Mid(Item.Subject, 6)
Item.Save
End If
End If
End Sub
Private Sub olkApp_NewMailEx(ByVal EntryIDCollection As String)
Dim arrEID As Variant, varEID As Variant, olkItm As Object
arrEID = Split(EntryIDCollection, ",")
For Each varEID In arrEID
Set olkItm = Outlook.Session.GetItemFromID(varEID)
If olkItm.Class = olMail Then
Select Case Left(olkItm.Subject, 4)
Case "FW: ", "RE: "
olkItm.Subject = Mid(olkItm.Subject, 5)
olkItm.Save
End Select
End If
Next
Set olkItm = Nothing
End Sub
Public Sub ToggleSend()
bolSend = Not bolSend
MsgBox "The process of removing RE: and FW: on sent messages has been turned " & IIf(bolSend, "'On'", "'Off'"), vbInformation + vbOKOnly, CLASS_NAME
End Sub
Public Sub ToggleReceive()
bolReceive = Not bolReceive
MsgBox "The process of removing 'RE:', 'FW:', and 'Fwd:' on received messages has been turned " & IIf(bolReceive, "'On'", "'Off'"), vbInformation + vbOKOnly, CLASS_NAME
End Sub
Part 2.
Follow these instructions to add part 2 of the code.
Outlook 2007.
- If not already expanded, expand Microsoft Office Outlook Objects and click on ThisOutlookSession
- Copy the code from the Code Snippet box and paste it into the right-hand pane of Outlook’s VB Editor window
- Click the diskette icon on the toolbar to save the changes
- Close the VB Editor
- Click Tools > Trust Center
- Click Macro Security
- Set Macro Security to “Warnings for all macros”
- Click OK
- Close Outlook
- Start Outlook. Outlook will display a dialog-box warning that ThisOutlookSession contains macros and asking if you want to allow them to run. Say yes.
Outlook 2010.
- If not already expanded, expand Microsoft Office Outlook Objects and click on ThisOutlookSession
- Copy the code from the Code Snippet box and paste it into the right-hand pane of Outlook’s VB Editor window
- Click the diskette icon on the toolbar to save the changes
- Close the VB Editor
- Click File and select Options
- When the Outlook Options dialog appears click Trust Center then click the Trust Center Settings button
- Click Macro Settings
- Select either of the two bottom settings (i.e. “Notifications for all macros” or “Enable all macros (not recommended; potentially dangerous code can run)”. The choice of which to choose is up to you. If you select “Notifications”, then you’ll be prompted at times to enable macros. If you pick “Enable all” then there’s a chance that a malicious macro could run. It’s a question of how much risk you want to assume.
- Click Ok until the dialog-boxes have all closed
- Close Outlook
- Start Outlook. Outlook will display a dialog-box warning that ThisOutlookSession contains macros and asking if you want to allow them to run. Say yes.
Dim objSendAndReceive As clsSendAndReceive
Private Sub Application_Quit()
Set objSendAndReceive = Nothing
End Sub
Private Sub Application_Startup()
Set objSendAndReceive = New clsSendAndReceive
End Sub
Public Sub ToggleSendState()
objSendAndReceive.ToggleSend
End Sub
Public Sub ToggleReceiveState()
objSendAndReceive.ToggleReceive
End Sub
Adding Buttons to Toggle the Send and Receive States
If you want to be able to turn checking on/off for sending and/or receiving, then you’ll need to add a pair of toolbar buttons for Outlook 2007 or buttons on the Quick Access Toolbar (QAT) for Outlook 2010. One button will call the macro ToggleSendState while the other will call ToggleReceiveState.
Outlook 2007. Follow these instructions to add toolbar buttons that run the macro.
Outlook 2010. Follow these instructions to add the macro to the QAT.
I have followed these instructions, but I cannot add the buttons to the toolbar because no macros show up in the list. I have Outlook 2007. After restarting Outlook and enabling Macros, then pressing Alt-F11, I can see the code that I pasted. What have I done wrong?
Hi, Mike.
In part 2 of the code change these two lines
to
Thanks for the help, David! I have buttons!
Great! You’re welcome, Mike.
This resolved a long standing request by our legal team, where words are everything. I had searched for a while to find the right script, but attempts by others simply didn’t work. David provided a flawless script, excellent instructions, and added enhanced functionality per the request, but fashioned much more elegantly than anticipated. Great work, and thank you!
Thanks for those very kind words, Robert. I’m glad I could help out.