Solved

Access VBA - modify code to lokk through only new mail

Posted on 2011-02-28
3
281 Views
Last Modified: 2012-05-11
Hi.
How would I modify the following code to look through only new mail items
Dim Olook As Outlook.Application
Dim oNs As Outlook.NameSpace
Dim oFldr As Outlook.MAPIFolder
Dim OMail As Outlook.MailItem
Dim LookFor As String


Set Olook = New Outlook.Application
Set oNs = Olook.GetNamespace("MAPI")
Set oFldr = oNs.GetDefaultFolder(olFolderInbox)



LookFor = "a"
Me.ListBox.RowSource = ""
For Each OMail In oFldr.Items
        With OMail
          If InStr(.Subject, LookFor) Then
          Me.List1.AddItem (OMail.Subject)
          End If
        End With
Next OMail
         
    Set OMail = Nothing
    Set oFldr = Nothing
    Set oNs = Nothing
    Set Olook = Nothing
0
Comment
Question by:murbro
3 Comments
 
LVL 31

Expert Comment

by:Helen_Feddema
Comment Utility
A better approach might be to use the Application_NewMail event of Outlook.  This event (located in the ThisOutlookSession class module) fires when a new mail message arrives.
0
 
LVL 9

Accepted Solution

by:
McOz earned 500 total points
Comment Utility
In your loop, add another check for the MailItem "unread" property:
For Each OMail In oFldr.Items
        With OMail
          If .Unread = true and InStr(.Subject, LookFor) Then
          Me.List1.AddItem (OMail.Subject)
          End If
        End With
Next OMail

Open in new window

0
 

Author Closing Comment

by:murbro
Comment Utility
Thanks very much

Thank you Helen_Feddema: for your input but already did that - this is additional code to manually run things to capture data that wasn't handled by that event
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Set OWA language and time zone in Exchange for individuals, all users or per database.
Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now