I have compiled numerous bits of code a quick hacks which I am making available here for anybody who is interested. The code is unmaintained, but I will endeavor to provide any help I can as time permits. All this code was created to complete a specific task and may be written very poorly with little to no documentation.

These are all released under the GPL license

spam code snippets

Outlooks Spam Handler

The spam filters that we use at work, process all the messages in a particular folder to train the filter. Rather than drag and drop messages, I use the following code to move the selected or active message into the target folder. For each of the public subs, I have a toolbar button which runs the code.

' Copyright under GPL by Mark Grimes

' Move selected mail to spam training folder
Public Sub Spam()
    Dim objSelection    As Variant
    Dim objDestFolder   As MAPIFolder

    Debug.Print "MoveToSpam..."

    Set objSelection = GetSelection
    Set objDestFolder = GetFolder("This is spam email")
    ProcessMessages objSelection, objDestFolder, True

    Debug.Print "Done"
End Sub

' Move selected mail to ham training folder
Public Sub Ham()
    Dim objSelection    As Variant
    Dim objDestFolder   As MAPIFolder

    Debug.Print "CopyToHam..."

    Set objSelection = GetSelection
    Set objDestFolder = GetFolder("This is legitimate email")
    ProcessMessages objSelection, objDestFolder, False

    Debug.Print "Done"
End Sub

' Move selected mail to whilelist training folder
Public Sub Whitelist()
    Dim objSelection    As Variant
    Dim objDestFolder   As MAPIFolder

    Debug.Print "Whitelist..."

    Set objSelection = GetSelection
    Set objDestFolder = GetFolder("Add to whitelist")
    ProcessMessages objSelection, objDestFolder, False

    Debug.Print "Done"
End Sub

' Return a collection which holds all the selected emails
Private Function GetSelection()
    Dim objApp, objSelection

    Set objApp = CreateObject("Outlook.Application")
    Set objSelection = objApp.ActiveExplorer.Selection
    Debug.Print "  got " & objSelection.Count & " items"

    Set GetSelection = objSelection
End Function

' Return the folder which we will move mail to
Private Function GetFolder(folder As String)
    Dim objNS           As NameSpace
    Dim objDestFolder   As MAPIFolder

    Set objNS = Application.GetNamespace("MAPI")
    Set objDestFolder = objNS.Folders.Item("Public Folders").Folders.Item("All Public Folders").Folders.Item("GFI AntiSpam Folders").Folders.Item(folder)
    Set GetFolder = objDestFolder
End Function

' Move or copy all the messages in the collection into the designated folder
Private Sub ProcessMessages(objSelection As Variant, objDestFolder As MAPIFolder, move As Boolean)
    Dim myItem As Object
    Dim myCopiedItem As Object

    For Each myItem In objSelection
        If Not (TypeOf myItem Is MailItem) Then
            Debug.Print "  item is not an email"
        Else
            If move Then
                Debug.Print "  moving item"
                myItem.move objDestFolder
            Else
                Debug.Print "  copying item"
                Set myCopiedItem = myItem.Copy
                myCopiedItem.move objDestFolder
            End If
        End If
    Next
End Sub

' Move current email to Spam folder
' Called from an open email rather than the list
Public Sub ThisIsSpam()
    Dim objSelection    As Variant
    Dim objDestFolder   As MAPIFolder

    Debug.Print "MoveToSpam..."

    Set objSelection = GetCurrentItem
    Set objDestFolder = GetFolder("This is spam email")
    ProcessMessages objSelection, objDestFolder, True

    Debug.Print "Done"
End Sub

' Return the current email as the sole member of a collection
Private Function GetCurrentItem()
    Dim objApp, objSelection, objItem

    Set objApp = CreateObject("Outlook.Application")
    Set objItem = objApp.ActiveInspector.CurrentItem
    Set objSelection = New Collection
    objSelection.Add objItem
    Debug.Print "  got " & objSelection.Count & " items"

    Set GetCurrentItem = objSelection
End Function

Outlook Junk Mail - Old

The following code worked for older versions of Outlook (2000 I believe), but does not work for newer versions. There used to be a junk button on the toolbar. The code effectively activated that button. I'm not sure how to do it in newer version of Outlook. I have actually given up on Outlook's spam filtering and use SpamAssassian now. You might check out Wininspector to track down the right object.

If anyone figures out a solution, please email me know. I have had several people ask about this.

This code combines the frequently used steps of adding the senders of all selected e-mails to the Outlook "Junnk Sender's List" and then moving the messages to the junk mail folder. I then create a toolbar button associated with this "macro."

The core of which is based on code from Sue Mosher's article in Windows & .Net Magazine and the kludge to access the unpublished "Add to Junk Senders" is from Rick Pearce's post to the microsoft.public.outlook.program_vba newsgroup.

' Copyright under GPL by Mark Grimes

Sub DealJunkMail()
    Dim objApp As Application
    Dim objSelection As Selection
    Dim blnDoIt As Boolean
    Dim intMaxItems As Integer
    Dim intOKToExceedMax As Integer
    Dim strMsg As String

    ' ### set your maximum selection size here ###
    intMaxItems = 5

    Set objApp = CreateObject("Outlook.Application")
    Set objSelection = objApp.ActiveExplorer.Selection
    Select Case objSelection.Count
        Case 0
            strMsg = "No items were selected"
            MsgBox strMsg, , "No selection"
            blnDoIt = False
        Case Is > intMaxItems
            strMsg = "You selected " & _
                objSelection.Count & " items. " & _
                "Do you really want to process " & _
                "that large a selection?"
            intOKToExceedMax = MsgBox( _
                Prompt:=strMsg, _
                Buttons:=vbYesNo + vbDefaultButton2, _
                Title:="Selection exceeds maximum")
            If intOKToExceedMax = vbYes Then
                blnDoIt = True
            Else
                blnDoIt = False
            End If
        Case Else
            blnDoIt = True
    End Select
    If blnDoIt = True Then

        ' ### set the procedure to run on the selection here ###
        Call AddToJunkAndMove(objSelection)

        Beep ' alert the user that we're done
        'MsgBox "All done!", , "Selection"
    End If
    Set objSelection = Nothing
    Set objApp = Nothing

End Sub

Sub AddToJunkAndMove(objSel As Selection)
    Dim objItem As Object
    Dim objNS As NameSpace
    Dim objDestFolder As MAPIFolder
    Dim myOlApp As Outlook.Application

    Set objNS = Application.GetNamespace("MAPI")
    Set objDestFolder = objNS.Folders.Item("Mailbox - Mark Grimes").Folders.Item("Junk E-mail")

    Set myOlApp = CreateObject("Outlook.Application")
    Dim ctl As CommandBarControl ' Junk E-mail flyout menu
    Dim subctl As CommandBarControl ' Add to Junk Senders list menu

    Set ctl = myOlApp.ActiveExplorer.CommandBars.FindControl(Type:=msoControlPopup, ID:=31126)
    Set subctl = ctl.CommandBar.Controls(1)
    'MsgBox subctl.Caption
    subctl.Execute

    For Each objItem In objSel
        If objItem.Class = olMail Then
            objItem.Move objDestFolder
        End If
    Next
    Set objDestFolder = Nothing
    Set objNS = Nothing
    Set objItem = Nothing
End Sub

Installing CRM114 on Cygwin

As is typical with installing anything in cygwin, CRM114 took some tweaking to get it to install properly. The CRM114 Discriminator is a "Controllable Regex Mutilator" according to their website. In engilish it is an complex and interesting tool to "examine incoming e-mail, system log streams, data files or other data streams, and to sort, filter, or alter the incoming files or data streams according to the user's wildest desires." Mostly it is used to sort spam.

Anyway, here is what I needed to do to install crm114 under cygwin. I used the 20040231-BlameYokohama version of CRM114. First, get the libiconv, gettext and procmail from cygwin. Then get the Mew from www.Mew.org (I used version 3.3.) Install mew-3.3 with cd bin && ./configure && make && make install. This will give you mewdecode and mewencode. From the CRM114 package cd into tre directory and install with ./configure && make && make install. Now, back in the main CRM114 dir, edit the Makefile to add the following lines somewhere after the initial definitions.

CFLAGS += -I/usr/local/include
LDFLAGS += -L/usr/local/lib
LIBS += -lintl -liconv

Finally, add $(LDFLAGS) to each of the compiliation step for the utilities (cssdiff, cssmerge, cssutil). It should look something like this:

cssutil: cssutil.c crm114.h crm114_structs.h crm114_config.h crm114_sysincludes.h
        $(CC)  $(CFLAGS) -c cssutil.c -o cssutil.o
        $(CC)  $(CFLAGS) $(LDFLAGS) cssutil.o -static -lm -ltre -o cssutil

Now run make and you should get everything compiled and left in the current directory.


Main

outlook

cygwin

perl

spam

vba

websites

excel

applescript

mac