How to Auto Remove URLs with a Specific Domain from Incoming Emails in Your Outlook

Some users wish Outlook to automatically remove URLs with a specific domain from the bodies of incoming emails. Although Outlook doesn’t offer such a direct function, it still can be realized with a bit of scripting. This article will introduce it in detail.

If you are reluctant to seeing the URLs with a specific domain in incoming emails, you can use the method shared in the article “How to Auto Move the Incoming Emails with Specific Hyperlinks to Junk E-mail Folder“. That means can auto mark the mails as spam, which will block the specific links. However, at times, you just want to let Outlook auto hide or remove the specific URLs. In this case, you can utilize the way shared in the followings instead.

Auto Remove URLs with a Specific Domain from Incoming Emails

  1. At the very outset, launch VBA editor in Outlook via “Alt + F11”.
  2. Then, copy the following VBA code into the “ThisOutlookSession” project.
Public WithEvents objInboxItems As Outlook.Items

Private Sub Application_Startup()
    Set objInboxItems = Outlook.Application.Session.GetDefaultFolder(olFolderInbox).Items
End Sub

'Occurs when new mail arrives in your Inbox
Private Sub objInboxItems_ItemAdd(ByVal Item As Object)
    Dim objMail As MailItem
    Dim objRegExp As Object
    Dim objMatchCollection As MatchCollection
    Dim objMatch As Match
    Dim strURL As String
 
    If TypeOf Item Is MailItem Then
       Set objMail = Item
 
       'Get URLs from Bodies with Regular Expressions
       Set objRegExp = New RegExp
       With objRegExp
           .Pattern = "(https?[:]//([0-9a-z=\?:/\.&-^!#$;_])*)"
           .Global = True
           .IgnoreCase = True
       End With
 
       If objRegExp.Test(objMail.Body) Then
          Set objMatchCollection = objRegExp.Execute(objMail.Body)
 
          For Each objMatch In objMatchCollection
              strURL = objMatch.SubMatches(0)
 
              'Remove the specific URL
              'Change "www.test.com" to your liking
              If InStr(strURL, "www.test.com") > 0 Then
                 objMail.Body = Replace(objMail.Body, strURL, "[URL Removed]")
                 objMail.Save
              End If
         Next
       End If
    End If
End Sub

VBA Code - Auto Remove URLs with a Specific Domain from Incoming Emails

  1. Afterwards, restart your Outlook to activate the macro.
  2. From now on, every time when a new email gets into your Inbox, Outlook will auto scan the email body for the URLs with a specific domain. If find it, Outlook will remove them and replace with “[URL Removed]”, shown as the following image.Removed URLs

Make Best Use of Outlook Inbox Repair Tool

Many users don’t know an excellent tool preinstalled in Outlook. That is Scanpst, a built-in Outlook fix tool. It is capable of solving various tiny glitches with ease. For instance, if your Outlook PST file becomes inaccessible for some reasons, you can firstly try it to repair PST. More often than not, as long as the issue is within the capability of inbox tool, Scanpst will succeed definitely. However, at times, the problems may exceed what Scanpst can do as well. At that point, you have to take aid of an experienced third party utility, such as DataNumen Outlook Repair.

Author Introduction:

Shirley Zhang is a data recovery expert in DataNumen, Inc., which is the world leader in data recovery technologies, including mdf recovery and outlook repair software products. For more information visit www.datanumen.com

Comments are closed.