Solved

Access VBA - modify code to lokk through only new mail

Posted on 2011-02-28
3
284 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 31

Expert Comment

by:Helen Feddema
ID: 34998075
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
ID: 34998076
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
ID: 34998784
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

729 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