Skip to content

Instantly share code, notes, and snippets.

@jeremywarrick
Last active March 31, 2020 09:14
Show Gist options
  • Select an option

  • Save jeremywarrick/36c7953708355dec06b3896f7c26c2bd to your computer and use it in GitHub Desktop.

Select an option

Save jeremywarrick/36c7953708355dec06b3896f7c26c2bd to your computer and use it in GitHub Desktop.
VBA to remove [EXTERNAL] tag in email subjects in Windows Outlook 2016

Overview

Want to remove prefixes from email subjects automatically before you send them? Let's do that...

Why would you want to do that

Let's say you're organization decides to add prefixes to inbound emails that are sent from domains other than your own and you want to remove them because you end up with stacked "RE: [EXTERNAL] FW: [EXTERNAL]" added to subjects, breaking the conversation view in Outlook... that's why.

Prerequisites

  • Windows 10
  • Outlook 2016
  • Ability to turn on macros in Outlook (you aren't prevented from doing so by an IT policy)

Steps

  1. Make sure you have the developer tab available in Outlook
  2. Click on 'Developer' in the Outlook ribbon
  3. Click on 'Visual Basic' to open the Visual Basic editor
  4. In the Visual Basic editor, on the left side in the 'Project' panel, expand the 'Microsoft Outlook Objects' tree and click on the 'ThisOutlookSession' to open the code editor
  5. Paste the following code in the code editor:
Option Explicit

Private WithEvents olInboxItems As Items

Private Sub Application_Startup()
  Dim objNS As NameSpace
  Set objNS = Application.Session
  ' instantiate objects declared WithEvents
  Set olInboxItems = objNS.GetDefaultFolder(olFolderInbox).Items
  Set objNS = Nothing
End Sub
 
Private Sub olInboxItems_ItemAdd(ByVal Item As Object)
  On Error Resume Next
  RemoveExternal Item
 
 End Sub
Function RemoveExternal(Item As Object)
Dim xSubject As String


If InStr(Item.Subject, "[EXTERNAL]") > 0 Then
    xSubject = Replace(Item.Subject, "[EXTERNAL]", "", , 1)
    Item.Subject = Trim(xSubject)
   
    ' MsgBox "New Subject -> " & Item.Subject, vbOKOnly
    Item.Save
End If
End Function
  1. Click 'Save' and exit the Visual Basic editor

How to use

Once you have the VBA macro set, it will automatically strip the '[EXTERNAL]' prefix from your email's subject on reciept.

I haven't bothered to deal with more complex cases like nested 'RE: FW:' because email clients handle them differently so this fix works if YOUR organization adds the prefix.

Credits to https://www.extendoffice.com/documents/outlook/5120-outlook-remove-subject-prefixes.html and https://www.slipstick.com/developer/save-attachments-to-the-hard-drive/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment