Outlook Object Model: Handling mailbox items – read or unread – from any folder

Being Exchange/Office developer, you make use of Outlook Object Model (OOM) API to can handle the mailbox items programmatically and process as per your wish. Let me walkthrough through this sample:

'Declaration block
Dim iOut As Outlook.Application
Dim iNs As Outlook.NameSpace
Dim iFldr As Outlook.MAPIFolder
Dim iCount As Integer
Dim iMsg As Outlook.MailItem
Dim iCtr As Long

'Create New Outlook Application
Set iOut = New Outlook.Application

'Get the MAPI Namespace
Set iNs = iOut.GetNamespace("MAPI")

'Select the folder you want to process
Set iFldr = iNs.PickFolder

'Print the values - No. of item count in the folder & No. of unread item count
Debug.Print "Total Items: " & iFldr.Items.Count
Debug.Print "Total Unread items = " & iFldr.UnReadItemCount

'Iterate the Messages
For Each iMsg In iFldr.Items

'Get the Message
        With iMsg

            'Print the fields
            Debug.Print .To
            Debug.Print .CC
            Debug.Print .Subject
            Debug.Print .Body

            'Check and print whether the item has been read or not
            If .UnRead Then
                Debug.Print "Message has not been read"
                Debug.Print "Message has been read"
            End If
            iCount = iCount + 1
        End With
    Next iMsg

'De-reference the objects whenever not used
    Set iMsg = Nothing
    Set iFldr = Nothing
    Set iNs = Nothing
    Set iOut = Nothing

You can see using Outlook Object Model (OOM) API is easier and implement your business logic easily.

Happy Programming!

Skip to main content