How to Auto Move the Incoming Emails with Specific Hyperlinks to Junk E-mail Folder

If you would like to auto move the incoming emails with specific hyperlinks in the message body to the Junk E-mail folder in your Outlook, you can utilize the method introduced in this article.

Sometimes, Outlook Junk email filter will mistakenly marks the genuine emails as junks. Thus, you may prefer to set the level of your Outlook junk email protection to low. However, in this case, many junk emails may not be recognized by Outlook junk email filter, such the emails with risky hyperlinks. Not only will it clutter up your inbox folder, but also it may infect your Outlook data if you click the links. In addition, if you dislike receiving the emails that contain specific hyperlinks, you may want to move them to the Junk E-mail folder as well. Therefore, here we will focus on this issue and guide you how to utilize VBA to auto move the incoming emails with specific hyperlinks to Junk E-mail folder.

Auto Move the Incoming Emails with Specific Hyperlinks to Junk E-mail Folder

Auto Move the Emails with Specific Hyperlinks to Junk E-mail Folder

  1. At the very outset, launch your Outlook application.
  2. Then in the main Outlook window, press “Alt + F11” key buttons.
  3. Subsequently, in the popup “Microsoft Visual Basic for Applications” window, open the “ThisOutlookSession” project.
  4. Next copy and paste the following VBA code into this project window.
Public WithEvents objIncomingItems As Outlook.Items

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

Private Sub objIncomingItems_ItemAdd(ByVal objItem As Object)
    Dim objMail As Outlook.MailItem
    Dim objWordDocument As Word.Document
    Dim objHyperlinks As Word.Hyperlinks
    Dim i As Long
    Dim strURL As String
    Dim objJunkMailFolder As Outlook.Folder
    Set objJunkMailFolder = Application.Session.GetDefaultFolder(olFolderJunk)
    If TypeOf objItem Is MailItem Then
       Set objMail = objItem
       Set objWordDocument = objMail.GetInspector.WordEditor
       Set objHyperlinks = objWordDocument.Hyperlinks
       If objHyperlinks.Count > 0 Then
          For i = objHyperlinks.Count To 1 Step -1
              strURL = objHyperlinks.Item(i).Address
              'Check if the hyperlink addresses contain specific words
              'You can change the condition as per your needs
              If InStr(LCase(strURL), "") > 0 Or InStr(LCase(strURL), "") > 0 Then
                 objMail.Move objJunkMailFolder
              End If
          Next i
       End If
    End If
End Sub

VBA Code - Auto Move the Incoming Emails with Specific Hyperlinks to Junk E-mail Folder

  1. After that, sign this code as usual.
  2. Later change Outlook macro settings to allow signed macros.
  3. Eventually restart your Outlook to enable the newly added VBA project.
  4. From now on, all the incoming emails which contain specific hyperlinks will be automatically moved to Junk E-mail folder.

PST Errors Can Occur Now and Then

Despite boasting of various powerful functions, Outlook still cannot get rid of PST errors. Therefore, it is an arduous, tedious but vitally important task for Outlook users to protect your PST data against loss. One of the most effective means is to back up your PST file on a regular basis, which will contribute a lot to future PST recovery after unanticipated PST corruption.

Author Introduction:

Shirley Zhang is a data recovery expert in DataNumen, Inc., which is the world leader in data recovery technologies, including fix SQL Server and outlook repair software products. For more information visit

4 responses to “How to Auto Move the Incoming Emails with Specific Hyperlinks to Junk E-mail Folder”

  1. I got the error corrected by adding the reference to Word.
    Still cannot get it to run
    I know I’m missing something simple. What?

  2. Having problems trying to implement this.
    I’m getting a
    VBA Compile error:
    User-defined type not defined
    OK Help

    Seems to be on the line:
    Dim objWordDocument As Word.Document

    I’m running Office 365 on Windows 11.
    Am I possibly missing a library?

    The only edit I did was on the If InStr( line to include a partial of the string I’m looking for.

    Can you help?

  3. I am trying to implement this but am not having any sucess.
    I copied the code (of course after reviewing it) and understand what it is doing.
    I’m familiar with vba and most of the functions you are using.
    The only thing I a bit confused about is if this will work on multiple mailboxes.
    I have 10 set up in Outlook 365, including an Outlook folder that was created at some point but as far as I know there is no email associated to it.
    What am I missing or doing incorrectly?

    thanks for any help
    I’m been looking for something like this for years.

Leave a Reply

Your email address will not be published. Required fields are marked *