Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loop through mailitems in inbox

Status
Not open for further replies.

thomasks

Programmer
May 12, 2006
113
US
I need a procedure to loop through the emails in my inbox and check for a line of text in the subject. Could anyone point me to a code snippet that shows how to do this with VBA in outlook?
 



Hi,

Have you tried the microsoft site?

Skip,
[sub]
[glasses] [red][/red]
[tongue][/sub]
 

This should give you a starting point:
Code:
[blue]For Each OneItem In Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Items
    Debug.Print OneItem.Subject
Next[/blue]

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

Professional Office Developers Association
 
Thanks All,
Here is what I came up with after poking around on the Microsoft web site.
Code:
[green][i]' for automatic syntax highlighting see faq102-6487 
[/i][/green]Option Explicit
Option Base 1
Private [b]Sub[/b] application_newmail()
    [b]Dim[/b] oNameSpace [b]As[/b] NameSpace
    [b]Dim[/b] oFolderInbox [b]As[/b] MAPIFolder
    [b]Dim[/b] oMailItem [b]As[/b] MailItem
    [b]Dim[/b] oMeeting [b]As[/b] MeetingItem
    [b]Dim[/b] msg [b]As[/b] [b]Object[/b]
    [b]On[/b] [b]Error[/b] [b]Resume[/b] [b]Next[/b]
    
        [green][i]'Load the variables with the information needed
[/i][/green]        [b]Set[/b] oNameSpace = Application.GetNamespace([navy]"MAPI"[/navy])
        [b]Set[/b] oFolderInbox = oNameSpace.GetDefaultFolder(olFolderInbox)

        oFolderInbox.Items.Sort [navy]"Received"[/navy], True
   
        [green][i]' Loop through all the messages in the inbox
[/i][/green]        [b]For[/b] [b]Each[/b] msg [b]In[/b] oFolderInbox.Items
        [green][i]' Process only mail messages
[/i][/green]        [b]If[/b] TypeOf msg [b]Is[/b] MailItem [b]Then[/b]
         
         [green][i]' Set the object oMailItem to the msg object currently referenced
[/i][/green]         [green][i]' in the loop
[/i][/green]         [b]Set[/b] oMailItem = msg
        
        [green][i]'Check the message subject for quote request
[/i][/green]        [green][i]'if it is, then create a task for it and delete the email
[/i][/green]        [b]If[/b] InStr(LCase(oMailItem.Subject), [navy]"pqr"[/navy]) > 0 [b]Or[/b] _
           InStr(LCase(oMailItem.Subject), [navy]"rfq"[/navy]) > 0 [b]Or[/b] _
           InStr(LCase(oMailItem.Subject), [navy]"ccqr"[/navy]) > 0 [b]And[/b] _
           InStr(LCase(oMailItem.Body), [navy]"estimator is kirk thomas"[/navy]) > 0 [b]Then[/b]
           
           [green][i]'This is where the actual task is created
[/i][/green]            [b]With[/b] oMailItem
                NewTask .SenderName, .Subject, .Body, .Attachments
            [b]End[/b] [b]With[/b]
           
           [green][i]'Delete the email since we don't need it any longer
[/i][/green]            oMailItem.Delete

            [b]End[/b] [b]If[/b]
    [b]End[/b] [b]If[/b]

    [b]Next[/b]
        [green][i]'Release the objects from memory after use
[/i][/green]        [b]Set[/b] oMailItem = [b]Nothing[/b]
        [b]Set[/b] oFolderInbox = [b]Nothing[/b]
        [b]Set[/b] oNameSpace = [b]Nothing[/b]

    
[b]End[/b] [b]Sub[/b]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top