Jump to content

Macro in outlook 365 to filter out "RE", "FWD" etc. from inbox and from sendt items.

MatsNorway

Edit: does the topic need to go into programming?

 

Been working hard at getting the code below to work, but i am still struggling with some details. I managed to run it with prompt, then bungled it. If this is of interest you need to make or get a microsoft certificate, allow macroes in regEdit and in Outlook.

 

Also i don't want to run prompt. So i just delete the lines now with with strikethrough?  Source: https://www.datanumen.com/blogs/auto-remove-prefix-re-fw-subject-replying-forwarding-emails/

 

Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
    Dim strSubject As String
 
    If InStr(Item.Subject, "RE") > 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 'RE'?", vbYesNo) = vbYes Then
          strSubject = Replace(Item.Subject, "RE:", "", vbTextCompare)
       Else
          strSubject = Item.Subject
       End If
    End If
 
    If InStr(Item.Subject, "FW") > 0 Then
       If MsgBox("Do you want to remove the prefix 'FW'?", vbYesNo) = vbYes Then
          strSubject = Replace(Item.Subject, "FW:", "", vbTextCompare)
       Else
          strSubject = Item.Subject
       End If
    End If
 
    Item.Subject = Trim(strSubject)
    Item.Save
End Sub

 

Also relevant: https://stackoverflow.com/questions/68442416/how-to-remove-re-or-fwd-or-ext-from-an-outlook-appointment-invitation

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

You can't just delete the if/else statements and expect the same result. Think of it this way, let's imagine the following statement:

if (condition)
  do x
else
  do y

 

If you remove the if/else statement but leave all the other instructions, you end up with this:

do x
do y

You end up doing both things of the choice you were given before deleting the if/else statement

 

By removing the If MsgBox statements, you're telling Outlook to do this:

strSubject = Replace(Item.Subject, "RE:", "", vbTextCompare)
strSubject = Item.Subject

 You're first assigning the subject line with the "RE:" removed but then immediately after that you assign the original subject line into the same variable.

 

If you really just blanketly want to replace all instances of "RE:" and "FW:" without bothering with a confirmation, you need something like this:

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

  If InStr(Item.Subject, "RE:") > 0 Then
  	strSubject = Replace(Item.Subject, "RE:", "", vbTextCompare)
  Else
  	strSubject = Item.Subject
  End If
  
  If InStr(strSubject, "FW:") > 0 Then
  	strSubject = Replace(strSubject, "FW:", "", vbTextCompare)
  End If
  
  Item.Subject = Trim(strSubject)
  Item.Save

End Sub

This solution will work if there's either of the prefixes anywhere in the subject line, but it'll delete all of them, regardless of position. If that's ok with you, it'll work, but if you want something that only checks the start of each subject line, you'd need to narrow down the position of the occurrences of your search term.

 

Also, note that I used strSubject in the second if-statement, because if I were to use Item.Subject in both and "FW:" is present somewhere in the subject line, the "RE:" prefix would just show up again if it'd take the original subject line to assign to the variable.

And now a word from our sponsor: 💩

-.-. --- --- .-.. --..-- / -.-- --- ..- / -.- -. --- .-- / -- --- .-. ... . / -.-. --- -.. .

ᑐᑌᑐᑢ

Spoiler

    ▄██████                                                      ▄██▀

  ▄█▀   ███                                                      ██

▄██     ███                                                      ██

███   ▄████  ▄█▀  ▀██▄    ▄████▄     ▄████▄     ▄████▄     ▄████▄██   ▄████▄

███████████ ███     ███ ▄██▀ ▀███▄ ▄██▀ ▀███▄ ▄██▀ ▀███▄ ▄██▀ ▀████ ▄██▀ ▀███▄

████▀   ███ ▀██▄   ▄██▀ ███    ███ ███        ███    ███ ███    ███ ███    ███

 ██▄    ███ ▄ ▀██▄██▀    ███▄ ▄██   ███▄ ▄██   ███▄ ▄███  ███▄ ▄███▄ ███▄ ▄██

  ▀█▄    ▀█ ██▄ ▀█▀     ▄ ▀████▀     ▀████▀     ▀████▀▀██▄ ▀████▀▀██▄ ▀████▀

       ▄█ ▄▄      ▄█▄  █▀            █▄                   ▄██  ▄▀

       ▀  ██      ███                ██                    ▄█

          ██      ███   ▄   ▄████▄   ██▄████▄     ▄████▄   ██   ▄

          ██      ███ ▄██ ▄██▀ ▀███▄ ███▀ ▀███▄ ▄██▀ ▀███▄ ██ ▄██

          ██     ███▀  ▄█ ███    ███ ███    ███ ███    ███ ██  ▄█

        █▄██  ▄▄██▀    ██  ███▄ ▄███▄ ███▄ ▄██   ███▄ ▄██  ██  ██

        ▀███████▀    ▄████▄ ▀████▀▀██▄ ▀████▀     ▀████▀ ▄█████████▄

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×