Joho the BlogSpam Domain Checker for Outlook - Joho the Blog

Spam Domain Checker for Outlook

A growing percentage of the 2,000+ spams I’m receiving every day come to false names at my domain, Here’s a VBA script for Outlook that searches the selected entries in a folder and moves bogus ones sent to that domain into a folder of your choice. To use it, create a folder to receive the putative spam; I’m calling it YOUR_SPAM_FOLDER in the script, but you should change it to whatver yours is called. Also, replace “” with the domain of your mail, and be sure to specify the addresses to that you want to accept. (And watch out for bad wraps in the code below.)

Public Sub CheckForBadEvidents()

Dim ToWhom As String
Dim objNS As NameSpace
Dim objInbox As MAPIFolder
Dim objSpamFolder As MAPIFolder

Set objNS = Application.GetNamespace("MAPI")
Set objInbox = objNS.GetDefaultFolder(olFolderInbox)
Set objSpamFolder = objInbox.Folders("YOUR_SPAM_FOLDER")
Dim objApp As Application
Dim objSelection As Selection
Set objApp = CreateObject("Outlook.Application")
Set objSel = objApp.ActiveExplorer.Selection
Dim objItem As Object

' check to make sure the folder is there
If objSpamFolder Is Nothing Then
MsgBox ("Folder not found")
Exit Sub
End If

x = 0 ' count the number of hits, for fun

For Each objItem In objSel
If objItem.Class = 43 Then ' if mail msg

' get the To line
ToWhom =
' If it's to but not
' to a recognized address...
If InStr(ToWhom, "") > 0 And _
(ToWhom <> "[email protected]" And _
ToWhom <> "[email protected]" And _
ToWhom <> "[email protected]") _

' move it
objItem.Move objSpamFolder
x = x + 1
End If
End If

MsgBox "Moved to YOUR_SPAM_FOLDER "    & x & _
" msgs with ill-formed addresses."

end Sub

Note: I’m an amateur and you use this script at your own risk. Really. I mean it. (The code for moving a msg to a folder came from here. I’ve lost where I cribbed the other functionality from.

Previous: « || Next: »

7 Responses to “Spam Domain Checker for Outlook”

  1. David, enough with the qualifications on your code! You obviously missed your calling and should have been a software engineer. My guess: you could have been the 3rd co-inventor of Google if you hadn’t gotten all those silly humanities degrees.

  2. I’ll bet he could do it in LISP if we challenged him…

  3. I want to try your script. Then… I copy and paste where? txt? save as .vba?
    Put where?

    from Brazil a hug,


  4. Suzana, in Outlook 2002, got to Tools->Macros->Visual Basic Editor. Create a module by going to Insert->Module. Paste the code into the module.

    To try it out, create the required folders in OL (not in Visual Basic), then go into your Inbox and select a couple of msgs. Then, in Visual Basic, put your text cursor anywhere into the code you just pasted in and press F5. That runs the code. You’ll probably get a msg asking if it’s ok to let a program access OL. Say yes. Pray.

    Once it’s working the way you want, Do File->Save in VB. Then go back to OL, and do Tools->Customize->Commands. You should see what you just created listed under Macros. Drag it into your OL toolbar. To use it, select msgs and press on your new button.

  5. Am i assuming correctly that this may only work in 2002.

    I did vba in my previous job, and am having trouble making this work in Outlook 2003.

    I know that there are differences between 2002 & 2003 (arent they always!), but am not aware how different (in code or features).

    Also, I dont use mapi, which I believe is part of my problem…I changed the relevant info to my domain and my ‘spam folder’ which was the ‘JUNK email’ folder I already had.

  6. Nevermind…I forgot that the folder needed to be directly in INBOX AND NOT JUST any other place in the mail groups>

    It worked just fine..


  7. ringtones for samsung phone…

Leave a Reply

Web Joho only

Comments (RSS).  RSS icon