Excel VBA Read Body of Unread Outlook Message with latest date

Hi

What VB.net code would I use to loop through the Unread messages of my Outlook Inbox
and find the last email with a specific subject then read the body.

Thanks
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

[ fanpages ]IT Services ConsultantCommented:
Title: "Excel VBA Read Body of Unread Outlook Message with latest date"

Question text:
---
What VB.net code would I use to loop through the Unread messages of my Outlook Inbox
and find the last email with a specific subject then read the body.
---

May I suggest that you revise the Topic from "MS Excel" to something relevant to your question (as you seem to be asking for VB.net code, but your title suggests otherwise)?
0
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Hi
I meant to say "What VBA code". My mistake
0
Saurabh Singh TeotiaCommented:
Mubro,

You can run this code to do what you are looking for..This will check first if the email is  unread in your inbox in additional to particular subject line... Now in order to run this code you need to have

Microsoft outlook 14.0 object library selected under tools-->reference in visual basic..

Now assuming the last email is unread and you have read all the earlier emails this will do what that...

Sub reademails()
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False

    Dim olNameSpace As Namespace, olApp As Outlook.Application
    Dim olFolder As MAPIFolder, olMail As Outlook.MailItem
    Dim rubj As String
    On Error GoTo 0
    Set olApp = New Outlook.Application
    Set olNameSpace = olApp.GetNamespace("MAPI")
    Set olFolder = olNameSpace.GetDefaultFolder(olFolderInbox)    '

    If olFolder Is Nothing Then Exit Sub    
    RSubj = "Your subject line here"

    For Each olMail In olFolder.Items
        On Error Resume Next
        If InStr(1, olMail.Subject, RSubj, vbTextCompare) > 0 And olMail.UnRead = True Then
            MsgBox olMail.Body
        End If
    Next olMail

    Set olApp = Nothing
    Set olNameSpace = Nothing
    Set olFolder = Nothing

    Application.DisplayAlerts = True
    Application.ScreenUpdating = True

End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Thanks very much
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.