Stripping RE:, FW:, and Fwd: from the Subject Line


 

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 ability to turn checking on/off for both sending and receiving with a simple toggle. By default, the script checks 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 must be open and running for scripts to work.

Instructions.

The code comes in two parts.

Part 1

Follow these instructions to add part 1 of the code.

  1. Start Outlook
  2. Press ALT + F11 to open the Visual Basic Editor
  3. If not already expanded, expand Microsoft Office Outlook Objects
  4. Right-click on Class Modules, select InsertClass Module
  5. In the Properties panel click on Name and enter clsSendAndReceive
  6. Copy the code from the code snippet box and paste it into the right-hand pane of Outlook’s VB Editor window
  7. Edit the code as needed. I included a comment where changes are needed or allowed.
  8. 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.

  1. If not already expanded, expand Microsoft Office Outlook Objects and click on ThisOutlookSession
  2. Copy the code from the code snippet box and paste it into the right-hand pane of Outlook’s VB Editor window
  3. Click the diskette icon on the toolbar to save the changes
  4. Close the VB Editor
  5. Click ToolsTrust Center
  6. Click Macro Security
  7. Set Macro Security to Warnings for all macros
  8. Click OK
  9. Close Outlook
  10. 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.

  1. If not already expanded, expand Microsoft Office Outlook Objects and click on ThisOutlookSession
  2. Copy the code from the code snippet box and paste it into the right-hand pane of Outlook’s VB Editor window
  3. Click the diskette icon on the toolbar to save the changes
  4. Close the VB Editor
  5. Click File and select Options
  6. When the Outlook Options dialog appears click Trust Center then click the Trust Center Settings button
  7. Click Macro Settings
  8. 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.
  9. Click Ok until the dialog-boxes have all closed
  10. Close Outlook
  11. 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.

Advertisements

45 comments on “Stripping RE:, FW:, and Fwd: from the Subject Line

  1. Hi David,
    I received an error in “Private WithEvents olkApp As Outlook.Application” invalid object.
    Do I need to activate the references in my outlook?
    Thank you

    • Hi, Wilson.

      That shouldn’t be necessary, but it’s worth checking to see if the reference to Outlook is active. If it’s not, then activate it.

    • Hi David,
      I managed to workout with invalid object. however when i tried to run it shows “Object Variable or With block variable not set” what does it mean?
      thx wilson

    • Is it possible to strip the existing [EXTERNAL] or FW: in the subject that has been received in outlook?

    • Hi, Wilson.

      The code should already be stripping “FW:”. I’m not sure what “[EXTERNAL]” means in this context. Please explain.

    • Hi David,
      Sorry for the confusion, what I mean is when I use this code ” Item.Subject = Replace(Item.Subject, “EXTERNAL”, “”). It only stripping incoming mail or send mail to outside. Is there anyway to strip the subject in the old mail that has “FW” or “External”.
      Thank you
      Wilson

    • Hi, Wilson.

      Ahh, now I understand. That will take something like the code below. You’ll need to select the folder you want to process before running the macro. It will then process all the items in the currently selected folder.

      Sub StripIt()
          Const MACRO_NAME = "StripIt"
          Dim olkFld As Object, _
              olkMsg As Object, _
              lngCnt As Long
          Set olkFld = Application.ActiveExplorer.CurrentFolder
          For Each olkMsg In olkFld.Items
              If olkMsg.Class = olMail Then
                  Select Case Left(olkMsg.Subject, 4)
                      Case "FW: ", "RE: "
                          olkMsg.Subject = Mid(olkMsg.Subject, 5)
                          olkMsg.Save
                          lngCnt = lngCnt + 1
                      Case "EXTERNAL"
                          olkMsg.Subject = Mid(olkMsg.Subject, 9)
                          olkMsg.Save
                          lngCnt = lngCnt + 1
                  End Select
              End If
          Next
          Set olkFld = Nothing
          Set olkMsg = Nothing
          MsgBox "Process complete.  I fixed " & lngCnt & " message(s).", vbInformation + vbOKOnly, MACRO_NAME
      End Sub
      
  2. David, Can you tell me if this is possible to do without an outlook client? I have a rule set up in OWA that forwards the message, but I’d like FW: to be invisible.

  3. Hi David – I was able to follow your instructions you sent to Brian on November 5, 2015 @ 6:02 am, and it removed the “[EXTERNAL]”, but it also removed the “Re: ” and the “Fw: “. Is there a way to only remove “[EXTERNAL]

    • Hi, Angie.

      Yes, you can remove just “[EXTERNAL]” with this version. Download the original code, then replace the olkApp_ItemSend subroutine with the version below.

      Private Sub olkApp_ItemSend(ByVal Item As Object, Cancel As Boolean)
          Item.Subject = Replace(Item.Subject, "[EXTERNAL]", "")
      End Sub
      
  4. Hi, Finally a fairly simple solution with great instructions. Thank you.
    My sent emails go out without the Re: or Fw: now which is great.
    However, for some reason, they still come in with the Re: and was wondering if this is because we connect to office365 or something.
    Implemented the code as specified (without the buttons addition).

    • Hi, Greg.

      Apologies for taking so long to respond.

      When you run the toggling macro, do you get the dialog-box telling you the state of the switch?

  5. I have scrupulously followed your instructions (2010 version) but the code seems to do nothing – the toggle buttons work though. Is this because I am trying to do this in Outlook 2013 and Outlook 2016? Does the code need to change to work on these versions of Outlook?

    • Hi, Bruce.

      No, the code doesn’t need to change for Outlook 2013 or 2016. You say that the toggle buttons are working. I’m assuming you know that because when you click them you get the pop up message telling you that the process of removing has been turned on or off, correct? If so, then that tells me that everything is set up correctly. The next step is to figure out if the code is functioning. To do that, I’ve added code to display some pop up dialogs as the code runs. Please replace the code from Part 1 of the instructions with the code below. Once you’ve done that, close and restart Outlook. On starting you should see a dialog-box saying “Process initialized”. If you do, then the code is running and monitoring your mailbox. Now, send yourself, or have someone send you, a message that has “RE:” or “FW:” in the subject. When that message arrives you should get another dialog-box saying “Something arrived”. If the item is an email, then the next dialog-box should say “Item is an email”. Finally, if the subject line contains “RE:” or “FW:”, then you should get a final dialog-box saying “Subject was changed”. Let me know if you get all of those dialog-boxes. If not, I need to know which ones didn’t appear.

      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
          MsgBox "Process initialized", vbInformation + vbOKOnly, CLASS_NAME
      End Sub
       
      Private Sub Class_Terminate()
          Set olkApp = Nothing
          MsgBox "Process terminated", vbInformation + vbOKOnly, CLASS_NAME
      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
          MsgBox "Something arrived", vbInformation + vbOKOnly, CLASS_NAME
          arrEID = Split(EntryIDCollection, ",")
          For Each varEID In arrEID
              Set olkItm = Outlook.Session.GetItemFromID(varEID)
              If olkItm.Class = olMail Then
                  MsgBox "Item is an email", vbInformation + vbOKOnly, CLASS_NAME
                  Select Case Left(olkItm.Subject, 4)
                      Case "FW: ", "RE: "
                          olkItm.Subject = Mid(olkItm.Subject, 5)
                          olkItm.Save
                          MsgBox "Subject was changed", vbInformation + vbOKOnly, CLASS_NAME
                  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
      
    • Thank you David and I have tried that.

      I have realised that part of the problem (but not quite all) is that I was being stupid! I thought the macro worked by stripping out text in the message as I composed it. Now I see that it works when it is sent/received – which is obvious really, but it just did not occur to me to try it like that!

      So almost everything now seems to work in Outlook 2016 (I will not have access to 2013 until Monday), but not quite. When I forward something to myself the last dialogue box does not appear, but sent and received messages have been stripped properly. When I reply to myself all dialogue boxes appear, but the copy sent message still had RE: in it (the received message did not).

    • Hi, Bruce.

      No, there’s no way I know of to remove things as they are typed. It only works when the message is sent.

    • I think I was being dense. I assumed the script worked as soon as I closed my draft e-mail. Now that I have understood, it all works well in 2016 (I will only get access to 2013 on Monday), with two exceptions.

      First, your modified script did not show the final prompt (subject removed) when forwarding an e-mail.

      More importantly the copy of any reply filed in my sent box still has RE: in it – the outgoing message itself does not. But if I forward a message, neither the message nor the copy in my sent box has FW: in it any more.

      I have been manually removing these pesky things for a long time – not to have to do it any more is fantastic. Thank you for all this hard work.

  6. Hello – Wondering whether this can be implemented on the mail server side instead of the outlook client side? We are looking to strip out a string that an app appends to the subject line each time it sends out an email. for instance “RE: questions [interactionId: 123123123213123]”. We want exchange to strip out the interaction id and the trailing numbers before sending out to the recipient.

    • Hi, Dan.

      No, this solution will not work on the mail server. To do this at the mail server you’ll need need to write an event-sink. I don’t dabble in those. They are best left to professional Exchange developers since a poorly written or buggy event-sink could bring the entire server down.

  7. Will this code work with subject lines with multiple RE: and in lower and upper case, for example an actual email in my inbox starts Re: RE: RE: RE:Re: Message Subject ?
    I work with a lot of Chinese customers and for some reason every time they reply an extra RE or Re is added and it’s very annoying.

    Also my company marks all eternal emails with “EXTERNAL:” how difficult would it be to modify the code to remove this as well?

    A final question, my understanding is this check all new emails being recieved, how could i get it to run through all the messages currently in my inbox ?

    • Hi, Paul.

      No, at present it will only strip RE: if it finds those characters at the beginning of the line. That’s to prevent it from removing an “re:” later in the line that you might want. I can change that behavior though. I can also have it remove “EXTERNAL”, but here too it will remove all instances of “EXTERNAL”. Do you want me to limit it to removing the word only when it’s in all caps?

    • Thanks for the reply David, I have been working on test code in Excel VBA on how to remove multiple occurences of Re: Fw: etc from a string using a Do While loop and InStr. Got some code working that did that so was going to try and incorporate the code into your code but had not yet entered your code into Outlook. When I tried entering you code in Outlook 2010 I keep getting an error when staring Outlook, the error is ;

      Compile Error: Invalid outside procedure

      Which appears to be caused by;

      Dim objSendAndReceive As clsSendAndReceive

      Any idea what I’m doing wrong?

      The way I was going to alter your code is below, the bit of code I’ve entered into your code works with some test data in excel, I have done some simple macros in excel VBA but don’t have any VBA in Outlook experience so was more comfortable writing test code in Excel.

      Private Sub olkApp_NewMailEx(ByVal EntryIDCollection As String)
          Dim arrEID As Variant, varEID As Variant, olkItm As Object
          Dim SubjectString As String
          Dim LCaseSubjectString As String
          Dim fwTest As Long
          Dim reTest As Long
          Dim fwdTest As Long
          Dim EXTERNALTest As Long
          arrEID = Split(EntryIDCollection, ",")
          
          For Each varEID In arrEID
              Set olkItm = Outlook.Session.GetItemFromID(varEID)
              If olkItm.Class = olMail Then
                      
                  SubjectString = olkItm.Subject
                  LCaseSubjectString = LCase(SubjectString)
                  fwTest = InStr(LCaseSubjectString, "fw: ")
                  reTest = InStr(LCaseSubjectString, "re: ")
                  fwdTest = InStr(LCaseSubjectString, "fwd: ")
                  EXTERNALTest = InStr(LCaseSubjectString, "external: ")
                      Do
                          If fwTest = 1 Or reTest = 1 Then
                              olkItm.Subject = Mid(CellString, 5)
                              olkItm.Save
                              SubjectString = olkItm.Subject
                              LCaseSubjectString = LCase(SubjectString)
                              fwTest = InStr(LCaseSubjectString, "fw: ")
                              reTest = InStr(LCaseSubjectString, "re: ")
                              fwdTest = InStr(LCaseSubjectString, "fwd: ")
                              EXTERNALTest = InStr(LCaseSubjectString, "external: ")
                          End If
                          If fwdTest = 1 Then
                              olkItm.Subject = Mid(CellString, 6)
                              olkItm.Save
                              SubjectString = olkItm.Subject
                              LCaseSubjectString = LCase(SubjectString)
                              fwTest = InStr(LCaseSubjectString, "fw: ")
                              reTest = InStr(LCaseSubjectString, "re: ")
                              fwdTest = InStr(LCaseSubjectString, "fwd: ")
                              EXTERNALTest = InStr(LCaseSubjectString, "external: ")
                          End If
                              If EXTERNALTest = 1 Then
                              olkItm.Subject = Mid(CellString, 6)
                              olkItm.Save
                              SubjectString = olkItm.Subject
                              LCaseSubjectString = LCase(SubjectString)
                              fwTest = InStr(LCaseSubjectString, "fw: ")
                              reTest = InStr(LCaseSubjectString, "re: ")
                              fwdTest = InStr(LCaseSubjectString, "fwd: ")
                              EXTERNALTest = InStr(LCaseSubjectString, "external: ")
                              
                          End If
                  Loop While fwTest = 1 Or reTest = 1 Or fwdTest = 1 Or EXTERNALTest = 1
              End If
          Next
          Set olkItm = Nothing
      End Sub
      
    • Hi, Paul.

      Can you post a screenshot of the code you have now? I don’t see the line that’s causing the error in the code you posted, so I’m guessing that you have additional code. If I could see the code as you have it now, then I’d have a better chance of finding the problem.

    • Hi, Brian.

      Sure. Download the original code then replace the olkApp_ItemSend subroutine with the version below. Leave the rest of the code as is. This version will continue to remove “FW:”, “RE:”, and “Fwd:” from the subject while also removing “EXTERNAL”.

      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
          End If
          If Left(Item.Subject, 5) = "Fwd: " Then
              Item.Subject = Mid(Item.Subject, 6)
              Item.Save
          End If
          Item.Subject = Replace(Item.Subject, "EXTERNAL", "")
      End Sub
      
    • Thanks David. This works great for emails I send out. Does this also work to remove the “[EXTERNAL]” from the subject line of incoming emails? Is there a simple modification I can make?

    • Hi, Brian.

      That code works for messages you send and those you receive. To remove the string “[EXTERNAL]” from the subject line of messages you receive, replace the olkApp_NewMailEx subroutine in the original code with the version below. Leave the rest of the code as is.

      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
              olkItm.Subject = Replace(olkItm.Subject, "[EXTERNAL]", "")
              olkItm.Save
          Next
          Set olkItm = Nothing
      End Sub
      
  8. Hi, i encountered an compiler error trying to put in the ThisOutlookSession code. It says it’s expecting user-defined type, not project. And the portion “…objSendAndReceive As clsSendAndReceive” is highlighted. What’s wrong and how can i remedy this ? Thanks.

    cheers !

    kiat.

    • Hi, Kiat.

      The error suggests that the class module (i.e. the code in the first part) isn’t named clsSendAndReceive as it needs to be. Please check the class module’s name.

  9. 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?

  10. 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!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s