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 Emails with Specific Hyperlinks to Junk E-mail Folder
- At the very outset, launch your Outlook application.
- Then in the main Outlook window, press “Alt + F11” key buttons.
- Subsequently, in the popup “Microsoft Visual Basic for Applications” window, open the “ThisOutlookSession” project.
- 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), "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
- After that, sign this code as usual.
- Later change Outlook macro settings to allow signed macros.
- Eventually restart your Outlook to enable the newly added VBA project.
- 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 www.datanumen.com
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?
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?
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.