Auto Remove [EXTERNAL] from subject - Issue with Macro

dextryn

Member
Outlook version
Outlook 365 64 bit
Email Account
Office 365 Exchange
Hello, I'm trying to get a macro working from a closed thread in this forum.
Here is the link to the thread.

My company recently instituted the policy that puts the [EXTERNAL] tag in every subject when an email is received from outside the organization. When it's only one email, it's not really an issue. However, when it turns into a thread you get [EXTERNAL]RE: [EXTERNAL]FW: [EXTERNAL] etc. To the point where you can't even read the subject without opening up the email.

I'd love to use this macro to strip the [EXTERNAL] from the subject line, and by that linked thread, it seems it's completely possible. Unfortunately, I'm brand new to the macro world and I need a lot of help.

I've set up the developer tab and hit the visual basic button, but after that I'm lost. I've tried to google "how to write a macro in outlook", but even that seems daunting. Could anyone help me set up the macro from the linked thread?
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
This should do it - it's the macro with the edits. This runs on all messages in the folder. You need to run it.

These macros were updated and tested.

Code:
Sub RemoveExternalString()
Dim myolApp As Outlook.Application
Dim Item As Object

Set myolApp = CreateObject("Outlook.Application")
Set mail = myolApp.ActiveExplorer.CurrentFolder

' Remove from left or right
Dim iItemsUpdated As Integer
Dim lString As Integer

iItemsUpdated = 0
For Each Item In mail.Items
strSubject = LCase(Item.Subject)
If InStr(1, strSubject, "[external] ") Then
  Item.Subject = Replace(strSubject, "[external] ", "")
  olItem.Save
End If
Next Item

MsgBox iItemsUpdated & " of " & mail.Items.Count & " Messages Updated"
Set myolApp = Nothing
End Sub


This is an automatic version - watches all messages as they arrive in the inbox and remove external - it should replace all uses of it - so in your example of [EXTERNAL]RE: [EXTERNAL]FW: [EXTERNAL] Original subject, it should revert to RE: FW: original subject.

Code:
Private objNS As Outlook.NameSpace
Private WithEvents objItems As Outlook.Items

Private Sub Application_Startup()

Dim objFolder As Outlook.folder
Set objNS = Application.GetNamespace("MAPI")

'Use default Inbox to watch:
Set objFolder = objNS.GetDefaultFolder(olFolderInbox)
Set objItems = objFolder.Items

Set objFolder = Nothing
End Sub


Sub objItems_ItemAdd(ByVal Item As Object)

strSubject = LCase(Item.Subject)
If InStr(1, strSubject, "[external] ") Then
  Item.Subject = Replace(strSubject, "[external] ", "")
  olItem.Save
End If
 
End Sub


 
Last edited:

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
The earlier macros weren't working and I updated the previous post with code that does work.
 

dextryn

Member
Outlook version
Outlook 365 64 bit
Email Account
Office 365 Exchange
Thank you so much for the help! It runs this time, but I get a message that says "0 out of ##" messages have been updated. Is there something I did wrong? I clicked on "Visual Basic" in the Developer tab, right clicked on "Project1" and selected "Insert" - "Module". Then I just copied/pasted the code into the text box.
After that I went back into my outlook and selected a folder and clicked "Macros" and the selected "Project1.RemoveExternalString"
I have to correct one thing, the external tag is [External] not [EXTERNAL]. I changed it in the script but it still did not work.
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
This:
strSubject = LCase(Item.Subject)
Takes care of different cases - upper, lower proper. Use lower case when looking for it and replacing -

If InStr(1, strSubject, "[external] ") Then
Item.Subject = Replace(strSubject, "[external] ", "")


You are using the new macros? I replaced my original ones with the ones above - or I thought I did - the manual one is not correct - its the one that gives you a count.

This part changed in the automatic one (the second one)
Code:
Sub objItems_ItemAdd(ByVal Item As Object)

strSubject = LCase(Item.Subject)
If InStr(1, strSubject, "[external] ") Then
  Item.Subject = Replace(strSubject, "[external] ", "")
  olItem.Save
End If
 
End Sub


This is the correct manual one - (also fixed above)

Code:
Sub RemoveExternalString()
Dim myolApp As Outlook.Application
Dim Item As Object

Set myolApp = CreateObject("Outlook.Application")
Set mail = myolApp.ActiveExplorer.CurrentFolder

' Remove from left or right
Dim iItemsUpdated As Integer
Dim lString As Integer

iItemsUpdated = 0
For Each Item In mail.Items
strSubject = LCase(Item.Subject)
If InStr(1, strSubject, "[external] ") Then
  Item.Subject = Replace(strSubject, "[external] ", "")
  olItem.Save
End If
Next Item

MsgBox iItemsUpdated & " of " & mail.Items.Count & " Messages Updated"
Set myolApp = Nothing
End Sub
 

dextryn

Member
Outlook version
Outlook 365 64 bit
Email Account
Office 365 Exchange
I'm not sure what I'm doing wrong. I've taken some screenshots to show how I have it and the error.
Is there a setting somewhere in Outlook where I need to give more permissions? I have "Enable all macros" selected (only for this purpose, I plan on digitally signing it and putting the protections back after I can figure this out).
Macro1.png
macro2.png
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
It's not macro permissions- it runs, so they are good. You have the inbox selected? Iwonder if it's the space - remove the space after external -
If InStr(1, strSubject, "[external]") Then
Item.Subject = Replace(strSubject, "[external]", "")
 

dextryn

Member
Outlook version
Outlook 365 64 bit
Email Account
Office 365 Exchange
That's it!!! It works!! Thank you so much for your help!
 

dextryn

Member
Outlook version
Outlook 365 64 bit
Email Account
Office 365 Exchange
Ok, actually. This is weird. Do you know why it would change the entire subject line to lowercase after it stripped out the External tag?
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
oh shoot, back to the drawing board. You'll need to use the correct case and hope other companies don't use a different case....

If InStr(1, Item.Subject, "[EXTERNAL]") Then
Item.Subject = Replace(Item.Subject, "[EXTERNAL]", "")
Item.Save
End If


If you have messages with different external tags this should work -

strSubject = LCase(Item.Subject)
If InStr(1, strSubject, "[external]") Then
Item.Subject = Replace(Item.Subject, "[EXTERNAL]", "")
Item.Subject = Replace(Item.Subject, "[External]", "")
Item.Save
End If
 

dextryn

Member
Outlook version
Outlook 365 64 bit
Email Account
Office 365 Exchange
That's it. I really only need this in my organization as the external tag can multiply when replying several times in the same thread. This fixes the issue for me. Again, thank you so much for your help.
 
Similar threads
Thread starter Title Forum Replies Date
P [SOLVED] Auto remove [EXTERNAL] from subject Using Outlook 16
A Auto Insert of filename when selecting 'Remove Attachment' Using Outlook 1
T Outlook 2010 manually actioning of send and receive? remove auto actioning Using Outlook 4
J Outlook's auto time zone update for meeting requestsHow to REMOVE or DISABLE? Using Outlook 1
H remove auto added signature macro Outlook VBA and Custom Forms 1
richardwing Auto forward email that is moves into a specific outlook folder Outlook VBA and Custom Forms 3
nmanikrishnan Auto-reply from default account Using Outlook 1
A Imap account not auto syncing inbox at startup Using Outlook 0
K Run a script rule to auto 'send again' on undeliverable emails? Outlook VBA and Custom Forms 1
FryW Need help modifying a VBA script for in coming emails to auto set custom reminder time Outlook VBA and Custom Forms 0
S Auto forward for multiple emails Outlook VBA and Custom Forms 0
DDB VBA to Auto Insert Date and Time in the signature Outlook VBA and Custom Forms 2
V Auto-complete stopped working Using Outlook 4
D auto forward base on email address in body email Outlook VBA and Custom Forms 0
M Replyall macro with template and auto insert receptens Outlook VBA and Custom Forms 1
R Auto Forwarding with different "From" Outlook VBA and Custom Forms 0
P auto-complete is hopelessly broken Using Outlook 0
R Auto Assign Category colours to Incoming Emails based on whom the email is addressed Outlook VBA and Custom Forms 3
C Auto Run VBA Code on new email Outlook VBA and Custom Forms 1
S Outlook Macro to send auto acknowledge mail only to new mails received to a specific shared inbox Outlook VBA and Custom Forms 0
V Auto-Submitted: auto-replied in header Using Outlook 0
R Auto display of new email does not work on non-default account Outlook VBA and Custom Forms 0
B Outlook 2016 Auto-archive creates new folder Using Outlook 3
J Edit auto-complete list in Outlook 2016+/365? Using Outlook 0
P Auto assign shared mailbox Outlook VBA and Custom Forms 1
M Outlook 2010 Problem with OutLook 2010 32 bit, after Windows Auto Update Using Outlook 3
Z Add text to auto-forwarded e-mail Outlook VBA and Custom Forms 4
N Disable Auto Read Receipts sent after using Advanced Find Using Outlook 4
Q Prompt button to auto turn on Out of Office Outlook VBA and Custom Forms 3
P Auto Insert Current Date or Time into Email Subject Outlook VBA and Custom Forms 2
S Messages moved / deleted by auto-archive are not synchronized to exchange Exchange Server Administration 8
B Outlook 2010 is Auto Purging when not configured for that Using Outlook 1
M VBA to auto forward message with new subject and body text Outlook VBA and Custom Forms 8
A Auto Accept Meetings from the General Calendar Using Outlook 3
R auto send email when meeting closes from a shared calendar only Outlook VBA and Custom Forms 2
S auto-mapping mailboxes in outlook impacting an ost file? Exchange Server Administration 2
M Auto expand Distribution List Before Sending Email Outlook VBA and Custom Forms 1
M Auto-export mail to Excel Outlook VBA and Custom Forms 2
Ms_Cynic Auto-pasting email content in calendar appt? Using Outlook 2
R How Do I insert images in and Auto Reply Using Outlook 3
S Received mail as part of DL, need to auto-CC the same when replying Outlook VBA and Custom Forms 5
T Have Outlook 2016 suggest email address auto complete entries directly from the user's contacts list Using Outlook 10
T Have Outlook 2016 suggest email address auto complete entries directly from the user's contacts list Using Outlook 0
P Auto scroll to specific folder in Folder Pane Outlook VBA and Custom Forms 3
C Auto categorize duplicate subjects Outlook VBA and Custom Forms 11
N Auto-complete - block select emails Using Outlook 3
C Auto save outlook attachments when email is received Outlook VBA and Custom Forms 1
J HELP- Rule to auto strip prepend from external emails Using Outlook 0
S BCM Auto Backup Data and Customizations BCM (Business Contact Manager) 6
G Auto accept meeting request for non primary account Outlook VBA and Custom Forms 1

Similar threads

Top