Results 1 to 6 of 6

Thread: Outlook VBA ----- ALEX--- help

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2001
    Location
    Austin
    Posts
    397

    Outlook VBA ----- ALEX--- help

    Have any of you all written any code that reads an email when it arrives in the inbox??

    I'm trying to develop a tool that will delete emails if certain words are in the body of the email....


    I know a wizard can do it but it's not getting all the words.
    Last edited by texas; Nov 17th, 2003 at 10:09 AM.

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2001
    Location
    Austin
    Posts
    397
    I found this in another thread.... Seems to be working.

    How could I replace the oEmail.Body, "xxxx" with a link to a db that has a large list of phrases that the macro could run through?

    Code:
    Private Sub Application_NewMail()
    
        Dim oNS As Outlook.NameSpace
        Dim oInbox As Outlook.MAPIFolder
        Dim oEmail As Outlook.MailItem
        Dim i As Integer
        
        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)
                If InStr(1, oEmail.Body, "enlarge your *****") > 0 Then
                    oEmail.Delete
                Else
                    If oEmail.UnRead = True Then oEmail.UnRead = False
                End If
                Set oEmail = Nothing
            End If
            Set oInbox = Nothing
            Set oInbox = oNS.GetDefaultFolder(olFolderInbox)
        Loop
    
        Set oInbox = Nothing
        Set oNS = Nothing
    
    End Sub
    Last edited by texas; Jun 30th, 2003 at 02:07 PM.

  3. #3
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    I think this should be it - think I should let you know this is untested though & if there are any errors thrown up by it, let me know!
    VB Code:
    1. Private Sub Application_NewMail()
    2.     Dim oNS As Outlook.NameSpace
    3.     Dim oInbox As Outlook.MAPIFolder
    4.     Dim oEmail As Outlook.MailItem
    5.     Dim i As Integer
    6.    
    7.     ' Set reference to microsoft activex data objects 2.6 library
    8.     ' from tools > references menu in Outlook.
    9.    
    10.     ' Declare ADO objects to talk to the database here
    11.     Dim cnnFilterDb As ADODB.Connection
    12.     Dim rsFilterWords As ADODB.Recordset
    13.    
    14.     Set oNS = Application.GetNamespace("MAPI")
    15.     Set oInbox = oNS.GetDefaultFolder(olFolderInbox)
    16.    
    17.     If oInbox.UnReadItemCount > 0 Then MsgBox "New Email!", vbOKOnly + vbInformation
    18.     Do While i < oInbox.Items.Count And oInbox.UnReadItemCount > 0
    19.         i = i + 1
    20.         DoEvents
    21.         If oInbox.UnReadItemCount > 0 Then
    22.             Set oEmail = oInbox.Items.Item(i)
    23.            
    24.             ' setup a connection to the database here. If you've not done this before,
    25.             ' I suggest you try looking up the ADO tutorial by Karl Moore on the Net.
    26.             ' If you need to build your own connection string for accessing SQL server etc.,
    27.             ' search the forums for any posts by me with 'udl' in.
    28.             Set cnnFilterDb = New ADODB.Connection
    29.            
    30.             cnnFilterDb.ConnectionString = "Provider = Microsoft.Jet.OLEDB.4.0; " & _
    31.             "DATA Source=C:\Path\MyAccessFileName.mdb;"
    32.             cnnFilterDb.Open
    33.            
    34.             ' setup a recordset object - for this purpose basically a reference or copy of
    35.             'your database table here, only looking at 1 single column.
    36.             Set rsFilterWords = New ADODB.Recordset
    37.             rsFilterWords.Open "SELECT fieldColumnFilterWordList FROM tableName" & _
    38.             cnnFilterDb, adOpenStatic, adLockReadOnly, adCmdText
    39.            
    40.             If oEmail.UnRead = True Then oEmail.UnRead = False
    41.            
    42.             ' Loop through all the records/rows in the recordset, if the word from the
    43.             ' currently looked at recordset row is found in the body of the e-mail, then
    44.             ' delete the mail and exit the loop (preventing all the other records from being
    45.             ' evaluated / looked at).
    46.             Do Until rsFilterWords.EOF
    47.                 If InStr(1, oEmail.Body, rsFilterWords!fieldColumnFilterWordList) > 0 Then
    48.                     oEmail.Delete
    49.                     Exit Do
    50.                 End If
    51.            
    52.                 ' move to the next record in the recordset which will be 'read' on the
    53.                 ' next time the loop goes round...
    54.                 rsFilterWords.MoveNext
    55.             Loop
    56.            
    57.             ' clean up unused ADO objects
    58.             Set rsFilterWords = Nothing
    59.             Set cnnFilterDb = Nothing
    60.            
    61.             Set oEmail = Nothing
    62.         End If
    63.         Set oInbox = Nothing
    64.         Set oInbox = oNS.GetDefaultFolder(olFolderInbox)
    65.     Loop
    66.  
    67.     Set oInbox = Nothing
    68.     Set oNS = Nothing
    69. End Sub

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2001
    Location
    Austin
    Posts
    397
    alex_read

    the code is working

    but

    i have one last question.


    would it be possible with the code you provided not to make the emails as "Read" currently the code is marking the emails as "Read" after it goes through them....
    Last edited by texas; Jul 1st, 2003 at 04:16 PM.

  5. #5
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    hmmm, okay try this one...
    VB Code:
    1. ' At the top of the code
    2. Dim blnMailWasDeleted As Boolean
    3.            
    4. ' Place this right before the loop
    5. blnMailWasDeleted = False
    6.            
    7. Do Until rsFilterWords.EOF
    8.     If InStr(1, oEmail.Body, rsFilterWords!fieldColumnFilterWordList) > 0 Then
    9.         blnMailWasDeleted = True
    10.         oEmail.Delete
    11.        
    12.         Exit Do
    13.     End If
    14.    
    15.     rsFilterWords.MoveNext
    16. Loop
    17.            
    18. ' If the mail didn't contain any of the words, then it wouldn't
    19. ' have been deleted & our boolean variable will be true
    20. If blnMailWasDeleted = False Then
    21.     oEmail.UnRead = True
    22. End If

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2001
    Location
    Austin
    Posts
    397
    is this how the code should look?

    Code:
    Private Sub Application_NewMail()
        Dim oNS As Outlook.NameSpace
        Dim oInbox As Outlook.MAPIFolder
        Dim oEmail As Outlook.MailItem
        Dim blnMailWasDeleted As Boolean
        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
            
                        blnMailWasDeleted = False
                            
                Do Until rsFilterWords.EOF
                    If InStr(1, oEmail.Body, rsFilterWords!fieldColumnFilterWordList) > 0 Then
                        blnMailWasDeleted = True
                        oEmail.Delete
                        
                        Exit Do
                    End If
                    
                    rsFilterWords.MoveNext
                Loop
                            
                ' If the mail didn't contain any of the words, then it wouldn't
                ' have been deleted & our boolean variable will be true
                If blnMailWasDeleted = False Then
                    oEmail.UnRead = True
                End If
                        
            
                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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width