Partager via


How to: Programmatically retrieve unread messages from the Inbox

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

This example retrieves unread email messages from the Outlook Inbox and displays the number of items.

Applies to: The information in this topic applies to VSTO Add-in projects for Outlook. For more information, see Features available by Office application and project type.

Example

Private Sub ThisAddIn_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup
    Dim inbox As Outlook.MAPIFolder = _
        Me.Application.ActiveExplorer().Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)

    Dim unreadItems As Outlook.Items = _
    inbox.Items.Restrict("[Unread]=true")

    MsgBox(String.Format("Unread items in Inbox = {0}", unreadItems.Count))
End Sub
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
    Outlook.MAPIFolder inbox =
        this.Application.ActiveExplorer().Session.GetDefaultFolder
        (Outlook.OlDefaultFolders.olFolderInbox);

    Outlook.Items unreadItems = inbox.
        Items.Restrict("[Unread]=true");

    MessageBox.Show(
        string.Format("Unread items in Inbox = {0}", unreadItems.Count));
}

See also