Pages

Tuesday, January 10, 2012

Browse through all Unread emails in Outlook Inbox and extract the information

If you want to run a loop through all the unread emails in your outlook inbox and capture the information like subject, receive date, body,etc .

Here is the code-

Sub browse_all_unread_emails_in_inbox_only()
'tools ->Refrence ->MICROSOFT OUTLOOK
'declare outlook objects
Dim olapp As Outlook.Application
Dim olappns As Outlook.Namespace
Dim oinbox As Outlook.Folder
Dim oitem As Outlook.MailItem
'set outlook objects
Set olapp = New Outlook.Application
Set olappns = olapp.GetNamespace("MAPI")
Set oinbox = olappns.GetDefaultFolder(olFolderInbox)
If oinbox.Items.Restrict("[UnRead] = True").Count = 0 Then
MsgBox "NO Unread Email In Inbox"
Exit Sub
End If
For Each oitem In oinbox.Items.Restrict("[UnRead] = True")
' u can add if condtions to filter the emails etc.
' u can add data it to excel sheet or database table
MsgBox "Mail Subject -> " & oitem.Subject
MsgBox "Sender Email Address -> " & oitem.SenderEmailAddress
MsgBox "Sender Name -> " & oitem.SenderName
MsgBox "Mail Body -> " & oitem.Body
MsgBox "Recived Date -> " & oitem.ReceivedTime
Next
End Sub

3 comments:

  1. Please do visit Below links to Learn Outlook VBA
    http://www.outlookcode.com
    http://www.jpsoftwaretech.com/

    If you came know any other Site please share with us.

    ReplyDelete
  2. Sub lastest_unread_email()

    'TOOLS ->Refrence -> microsoft outlook
    'declare outlook objects

    Dim olapp As Outlook.Application
    Dim olappns As Outlook.Namespace
    Dim oinbox As Outlook.Folder
    Dim oitem As Outlook.MailItem
    Dim myItems As Outlook.Items

    'set outlook objects
    Set olapp = New Outlook.Application
    Set olappns = olapp.GetNamespace("MAPI")
    Set oinbox = olappns.GetDefaultFolder(olFolderInbox)

    ' check if any unread email in inbox
    If oinbox.Items.Restrict("[UnRead] = True").Count = 0 Then
    MsgBox "NO Unread Email In Inbox"
    Exit Sub
    End If

    ' sort emails on recieved basis
    Set myItems = oinbox.Items
    myItems.Sort "[Received]", True

    'loop through all unread emails

    For Each oitem In myItems.Restrict("[UnRead] = True")
    'Display first unread email
    MsgBox "Sender " & oitem.SenderEmailAddress & vbNewLine & vbNewLine _
    & "Subject " & oitem.Subject & vbNewLine & vbNewLine _
    & "Recived " & oitem.ReceivedTime
    ' exit for loop
    Exit For
    Next

    End Sub

    ReplyDelete
  3. Here a code to give back the unread emails from different mailboxes.

    Sub ItemVerkoopBinnendienstAN()
    'Geeft het aantal ongeopende mails terug

    Dim olApp As Outlook.Application
    Dim objNamespace As Namespace
    Dim colFolders As Folders
    Dim objReadFolder As MAPIFolder
    Dim i As Integer
    Dim olMail As Variant

    Set olApp = New Outlook.Application
    Set objNamespace = olApp.GetNamespace("MAPI")
    Set colFolders = objNamespace.Folders
    Set objReadFolder = colFolders.Item("VerkoopBinnendienst (AN)")
    Set olinboxitems = objReadFolder.Folders.Item("Postvak IN").Items

    i = 0
    For Each olMail In olinboxitems
    If olMail.UnRead Then
    i = i + 1
    End If
    Next olMail

    Sheets("Blad1").Range("A1").Value = (i)

    Set olPublicFolder = Nothing
    Set olNs = Nothing
    Set olApp = Nothing

    End Sub

    ReplyDelete