Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Move the emails with spam hyperlinks to Junk E-mail folder

  1. May 16, 2017 #1

    I have a question about Outlook Junk email filter. There are some emails with a few spam hyperlinks which are not recognized by Outlook junk email filter. I have to move them to Junk E-mail folder manually.
    And I hope outlook can auto move those incoming email with a few specific spam hyperlinks to Outlook Junk email filter. How can I do that? Thanks.
  2. jcsd
  3. May 17, 2017 #2


    User Avatar
    Science Advisor
    Gold Member
    2017 Award

    Junk email filters don't generally look in the email. They just look at the subject or from address and apply the rules that you set along with others that the provider (Microsoft in this case) might also have.
  4. May 20, 2017 #3
    It seems that you can do that via VBA codes. I search on google and find one may work to you.

    Code (Text):
    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), "www.test.com") > 0 Or InStr(LCase(strURL), "www.sales.com") > 0 Then
                     objMail.Move objJunkMailFolder
                  End If
              Next i
           End If
        End If
    End Sub
    Good luck.
    Last edited by a moderator: May 20, 2017
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted