Solved

Access VBA - modify code to lokk through only new mail

Posted on 2011-02-28
3
283 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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
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 …

730 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