Removing [EXTERNAL] from Subject line of emails

Holley

Board Regular
Joined
Dec 11, 2019
Messages
155
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hello all and many thanks for all past help! I have a macro I am using that removed EXTERNAL from incoming emails. This is a new feature that is causing issues with other things and I need to remove it. I have a code that works just about exactly as I need. The only drawback is that is changes everything to lower case. I would like for this to retain the case as it is when the email is received. One thing I don't understand, but it isn't affecting the functionality of the macro, is that when the message box appears, it says 0 of ## when it obviously did update the subject lines. Not sure why that is... Any suggestions would be most appreciated. Here is the code I am currently using (picked up from another forum).
VBA 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] ", "")
Item.Save
End If
Next Item

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

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
You never increment the counter which is why it still says 0 at the end. Try this:

VBA 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 = Item.Subject
      If InStr(1, strSubject, "[external]", vbTextCompare) Then
         Item.Subject = Trim$(Replace(strSubject, "[external]", "", 1, -1, vbTextCompare))
         Item.Save
         iItemsUpdated = iItemsUpdated + 1
      End If
   Next Item
   
   MsgBox iItemsUpdated & " of " & mail.Items.Count & " Messages Updated"
   Set myolApp = Nothing
End Sub
 
Upvote 0
Solution
Oh my!! THIS IS PERFECT! Thank you so very much!! Greatly appreciated!!
 
Upvote 0

Forum statistics

Threads
1,223,249
Messages
6,171,031
Members
452,374
Latest member
keccles

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top