Replace Text in Email Subject

Tomas

Member
Outlook version
Outlook 2013 32 bit
Email Account
Exchange Server
Operating system::    Windows 10
Outlook version:     Outlook 365
Email type or host:    Exchange Online

Hello, Diane,

following code works great for replacement of prefixes AW:/WG: to Re:/Fwd: in any subject lines of my outgoing emails. Unfortunatelly, in case of new messages/messages without above mentioned prefixes, subject line will be completely removed and emails are sending with blank subject line.

Could you please check this code and get him to work properly? I tried to comment the line Item.Subject = Trim(strSubject). With this change subject line of new messages remains but AW:/WG: will not be replaced by Re:/Fwd:.

Your help will be highly appreciated.

Thank you in advance.

Code:
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
   'https://windowsreport.com/outlook-subject-prefix-fw-re/
   Dim strSubject As String
   If InStr(Item.Subject, "AW") > 0 Then
      'If you don't want the prompt,
      'You can remove the MsgBox line and its correspoding "Else … End If" lines.
      If MsgBox("Do you want to remove the prefix 'AW'?", vbYesNo) = vbYes Then
         strSubject = Replace(Item.Subject, "AW:", "Re:", vbTextCompare)
      Else
         strSubject = Item.Subject
      End If
   End If

   If InStr(Item.Subject, "WG") > 0 Then
      If MsgBox("Do you want to remove the prefix 'WG'?", vbYesNo) = vbYes Then
         strSubject = Replace(Item.Subject, "WG:", "Fwd:", vbTextCompare)
      Else
         strSubject = Item.Subject
      End If
   End If

   Item.Subject = Trim(strSubject)
   Item.Save
End Sub
 
Because of the two If statements, when neither "AW" nor "WG" occurs in the Item.Subject, then none of the four statements that assign a value to strSubject can be executed. Then execution goes to the statement Item.Subject = Trim(strSubject), but strSubject has the default value "" so Item.Subject is set to "" (that is, the empty string).

The simplest change that fixes this is to add the following statement immediately after the Dim statement:

strSubject = Item.Subject
 
It should work - it checks the first IF and set the subject - then does the second. Oh wait, it has nested IF statements and nothing indicating what to do if neither prefix is found.

If InStr(Item.Subject, "AW") > 0 Then
If MsgBox("Do you want to remove the prefix 'AW'?", vbYesNo) = vbYes Then
strSubject = Replace(Item.Subject, "AW:", "Re:", vbTextCompare)
Else
strSubject = Item.Subject
End If

End If

moving the strSubject line will work

If InStr(Item.Subject, "AW") > 0 Then
If MsgBox("Do you want to remove the prefix 'AW'?", vbYesNo) = vbYes Then
strSubject = Replace(Item.Subject, "AW:", "Re:", vbTextCompare)
End If
Else
strSubject = Item.Subject
End If

 
BTW - this is one better way of doing it. And if you need to remove other prefixes (like External:), add the prefix and the replacement in the arrays.

If you need to check for multiple prefixes, remove the Exit For line so it checks all values in the array.

Code:
Private Sub Application_ItemSend(ByVal item As Object, Cancel As Boolean)
   Dim strSubject As String
   Dim olPrefix As Variant
   Dim olReplace As Variant
   Dim i As Long
  
   olPrefix = Array("AW:", "WG:")
   olReplace = Array("Re:", "Fwd:")
  
   strSubject = item.Subject
  
    For i = 0 To UBound(olPrefix)
        If InStr(1, strSubject, olPrefix(i)) > 0 Then
          strSubject = Replace(strSubject, olPrefix(i), olReplace(i), vbTextCompare)
          Exit For
        End If
    Next
    
   item.Subject = Trim(strSubject)
   item. Save
End Sub

Another way would be to OR the two instr lines and put both replaces in the IF. (I did not test this one.)

Oh, and I would check for the colon too - not just the letters because it will find AW within a word - and your replace has the colon. (It's case-sensitive but there is always a chance someone will type all caps in the subject.)

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim strSubject As String

If InStr(Item.Subject, "AW:") > 0 OR InStr(Item.Subject, "WG:") > 0 Then

If MsgBox("Do you want to remove the AW or WG prefix?", vbYesNo) = vbYes Then
strSubject = Replace(Item.Subject, "AW:", "Re:", vbTextCompare)
strSubject = Replace(Item.Subject, "WG:", "Fwd:", vbTextCompare)
End if
Else
strSubject = Item.Subject
End If

Item.Subject = Trim(strSubject)
Item.Save
End Sub
 
Similar threads
Thread starter Title Forum Replies Date
divan Outlook 2007 - Replace email body with custom text Using Outlook 9
O Replace hard returns with soft returns on selected text and button to QAT Using Outlook 5
Beeto Replace a string in body Outlook VBA and Custom Forms 2
O How to find and replace a word in Outlook-Agenda-Subject and Message? Using Outlook 0
S HTML Code Embedded in String Within Open Outlook Email Preventing Replace(Application.ActiveInspector.CurrentItem.HTMLBody From Working Outlook VBA and Custom Forms 4
M Outlook 2013 Replace Subject with Conversation (a "hidden" value). Outlook VBA and Custom Forms 0
F Copy and replace not update contact in another pst Using Outlook 0
B Automatically Forward Emails and Remove/Replace All or Part of Body Outlook VBA and Custom Forms 8
C How to replace or delete first instance of sentence in mail body? Outlook VBA and Custom Forms 1
C replace subject line generated by converting a word document to PDF and sending it to an email Using Outlook 8
Diane Poremsky How to perform a global search and replace Using Outlook 0
B Delete/replace old files and save new attachments Using Outlook 1
Diane Poremsky Replace Display Names with Email Addresses Using Outlook 0
mrje1 Is there a Find and Replace feature in Outlook 2016? Using Outlook 4
J Pull an email address from body and replace reply-to address Outlook VBA and Custom Forms 4
E VBScript to replace module? Using Outlook 3
A How to replace column title in address book Using Outlook 1
V Replace only part of subject in OUTLOOK 2003 Outlook VBA and Custom Forms 1
A Replace olFolderContacts for Sharepoint lists in Outlook Outlook VBA and Custom Forms 5
P replace default outlook form with custom form Outlook VBA and Custom Forms 1
A How to replace getcontactsfolder (ol2007) in Outlook 2003 Outlook VBA and Custom Forms 2
P Email and calendar entry text now shifts right about 3 tabs worth of space Using Outlook 1
M Folder names - rich text Using Outlook 6
E Replying to a plain text e-mail with HTML Using Outlook 2
H Finding text in open email Outlook VBA and Custom Forms 12
D Can't read some emails - text size too small. Using Outlook 2
AndyZ Contact Custom Form Tiny Text Outlook VBA and Custom Forms 3
D Delete selected text in outgoing email body Outlook VBA and Custom Forms 0
kburrows Outlook Email Body Text Disappears/Overlaps, Folders Switch Around when You Hover, Excel Opens Randomly and Runs in the Background - Profile Corrupt? Using Outlook 0
J PSA: How to create custom keyboard shortcut for "Paste Unformatted Text" in Outlook on Windows Outlook VBA and Custom Forms 1
Witzker Add a text line at the end of the note field in all selected Contacts Outlook VBA and Custom Forms 7
J Outlook 365 Emails showing as links and text only Using Outlook 4
R How to force Outlook to use plain text in notes for Contacts? Using Outlook 1
J Text icon in Quick Access toolbar ? Using Outlook 2
S New Outlook Appointment - Select All Body Text and Change Font and Size Outlook VBA and Custom Forms 1
Z Copy specific email body text Outlook VBA and Custom Forms 0
L did MS ever add way to text via Outlook Using Outlook 5
B Outlook 365 Populate Outlook Task UDFs from a UDF text string Outlook VBA and Custom Forms 2
D Forwarding email based on the attachment file type and specific text found on the attachment file name Outlook VBA and Custom Forms 1
BartH Add a string to the conditions in .Conditions.BodyOrSubject.Text Outlook VBA and Custom Forms 2
S HTML to Plain Text Macro - Help Outlook VBA and Custom Forms 1
S Unable to extract text from an Outlook email message Using Outlook 2
T Original email text not shown when replying or forwarding the email. Using Outlook 9
M Outlook, send to > mail recipient - results in plain text email Using Outlook 1
P Forwarding emails issue with special characters replacing text body Using Outlook 1
J Autoreply email recieved from specific sender after deleting some text from body. Using Outlook 0
O Outlook tasks - Add text column with multiple lines Using Outlook 3
S Body text of Email from invite date/time Outlook VBA and Custom Forms 7
T Why does outlook 2010 convert only some forum notifications to plain text? Using Outlook 0
Z VBA to convert email to task, insert text of email in task notes, and attach copy of original email Outlook VBA and Custom Forms 4

Similar threads

Back
Top