[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Access VBA - modify code to lokk through only new mail

Posted on 2011-02-28
3
Medium Priority
?
289 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:Murray Brown
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 2000 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:Murray Brown
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

By default Outlook 2016 displays only one time zone in the Calendar. The following article explains how to display two time zones in one calendar view.
This article will help to fix the below errors for MS Exchange Server 2016 I. Certificate error "name on the security certificate is invalid or does not match the name of the site" II. Out of Office not working III. Make Internal URLs and Externa…
Many of my clients call in with monstrous Gmail overloading issues with Outlook. A quick tip is to turn off the All Mail and Important folders from synching. Here is a quick video I made to show you how to turn off these and other folders in Gmail s…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Suggested Courses

834 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