Private Sub Application_NewMail()
Dim oNS As Outlook.NameSpace
Dim oInbox As Outlook.MAPIFolder
Dim oEmail As Outlook.MailItem
Dim i As Integer
' Set reference to microsoft activex data objects 2.6 library
' from tools > references menu in Outlook.
' Declare ADO objects to talk to the database here
Dim cnnFilterDb As ADODB.Connection
Dim rsFilterWords As ADODB.Recordset
Set oNS = Application.GetNamespace("MAPI")
Set oInbox = oNS.GetDefaultFolder(olFolderInbox)
If oInbox.UnReadItemCount > 0 Then MsgBox "New Email!", vbOKOnly + vbInformation
Do While i < oInbox.Items.Count And oInbox.UnReadItemCount > 0
i = i + 1
DoEvents
If oInbox.UnReadItemCount > 0 Then
Set oEmail = oInbox.Items.Item(i)
' setup a connection to the database here. If you've not done this before,
' I suggest you try looking up the ADO tutorial by Karl Moore on the Net.
' If you need to build your own connection string for accessing SQL server etc.,
' search the forums for any posts by me with 'udl' in.
Set cnnFilterDb = New ADODB.Connection
cnnFilterDb.ConnectionString = "Provider = Microsoft.Jet.OLEDB.4.0; " & _
"DATA Source=C:\Path\MyAccessFileName.mdb;"
cnnFilterDb.Open
' setup a recordset object - for this purpose basically a reference or copy of
'your database table here, only looking at 1 single column.
Set rsFilterWords = New ADODB.Recordset
rsFilterWords.Open "SELECT fieldColumnFilterWordList FROM tableName" & _
cnnFilterDb, adOpenStatic, adLockReadOnly, adCmdText
If oEmail.UnRead = True Then oEmail.UnRead = False
' Loop through all the records/rows in the recordset, if the word from the
' currently looked at recordset row is found in the body of the e-mail, then
' delete the mail and exit the loop (preventing all the other records from being
' evaluated / looked at).
Do Until rsFilterWords.EOF
If InStr(1, oEmail.Body, rsFilterWords!fieldColumnFilterWordList) > 0 Then
oEmail.Delete
Exit Do
End If
' move to the next record in the recordset which will be 'read' on the
' next time the loop goes round...
rsFilterWords.MoveNext
Loop
' clean up unused ADO objects
Set rsFilterWords = Nothing
Set cnnFilterDb = Nothing
Set oEmail = Nothing
End If
Set oInbox = Nothing
Set oInbox = oNS.GetDefaultFolder(olFolderInbox)
Loop
Set oInbox = Nothing
Set oNS = Nothing
End Sub