Mark emails as read/unread using VBA in Outlook

Have you ever felt overwhelmed by the sheer number of unread emails in your Outlook inbox? Or perhaps you’ve read an email but want to mark it as unread to remind yourself to revisit it later.

Managing the read/unread status of emails can be a tedious task, especially when dealing with a large volume of messages.

This is where Visual Basic for Applications (VBA) comes into play, offering a practical solution for Outlook users.

In this blog post, we’ll explore how to use VBA in Outlook to mark emails as read or unread with ease.

VBA Code to Mark Emails as Read/Unread

Sub MarkEmailsReadOrUnread()
    Dim OutlookApp As Outlook.Application
    Dim Namespace As Outlook.NameSpace
    Dim Folder As Outlook.MAPIFolder
    Dim MailItem As Object
    Dim i As Integer

    Set OutlookApp = New Outlook.Application
    Set Namespace = OutlookApp.GetNamespace("MAPI")
    Set Folder = Namespace.GetDefaultFolder(olFolderInbox)

    For i = Folder.Items.Count To 1 Step -1
        Set MailItem = Folder.Items.Item(i)
        If TypeName(MailItem) = "MailItem" Then
            MailItem.UnRead = Not MailItem.UnRead
            MailItem.Save
        End If
    Next i
End Sub

Explaining the VBA Code

  1. Defining Objects:
    • Outlook.Application: Creates a new instance of Outlook.
    • Outlook.NameSpace: Accesses the MAPI namespace to interact with Outlook data.
    • Outlook.MAPIFolder: Represents the Inbox folder.
    • MailItem: Represents an individual email.
  2. Initializing Objects:
    • Set OutlookApp = New Outlook.Application: Starts Outlook.
    • Set Namespace = OutlookApp.GetNamespace("MAPI"): Connects to the MAPI layer.
    • Set Folder = Namespace.GetDefaultFolder(olFolderInbox): Targets the Inbox folder.
  3. Looping Through Emails:
    • For i = Folder.Items.Count To 1 Step -1: Iterates over each email in reverse order.
    • Set MailItem = Folder.Items.Item(i): Selects an individual email.
  4. Marking as Read/Unread:
    • If TypeName(MailItem) = "MailItem" Then: Checks if the item is an email.
    • MailItem.UnRead = Not MailItem.UnRead: Toggles the read/unread status.
    • MailItem.Save: Saves the changes.

Adding VBA Code to Outlook

  1. Open VBA Editor: In Outlook, press Alt + F11 to open the VBA editor or go to the Developer tab.
  1. Click on Visual Basic to open the VBA editor.
  1. Insert a New Module: In the editor, right-click on “ThisOutlookSession” → Choose “Insert” → “Module”.
  1. Paste the Code: Copy the provided VBA code into the new module.
  2. Save the Module: Press Ctrl + S to save, and close the editor

How to use VBA Code in Outlook to read/unread

Once you’ve added the VBA code to the Visual Basic Editor, you can effortlessly activate it in just a few seconds to execute the action.

Enable Macros

First, to activate macros, you need to modify some settings. Start by going to the “File” tab, then click on “Options.” Select “Trust Center” and proceed to click on “Trust Center Settings.” Next, navigate to “Macro Settings” and choose the option to “Enable all macros.”

Run the Script

Press Alt + F11 to open the VBA editor, find the script, and press F5 to run it.

Running the Script

  • Open Outlook, To ensure the script is ready.
  • Press Alt + F8 to execute the action.
  • Select MarkEmailsReadOrUnread.
  • Click “Run”.

Conclusion

Using VBA in Outlook to mark emails as read or unread is a simple yet powerful way to enhance your email management.

The VBA script provided in this post is a straightforward solution to automate what can often be a repetitive and time-consuming task.

By incorporating this script into your Outlook, you can save valuable time and focus on more important tasks.

Related Articles