How to: Programmatically Determine the Current Outlook Item


This example uses the Explorer.SelectionChange event to display the name of the current folder and some information about the selected item. The code then displays the selected item.

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.

    Dim WithEvents currentExplorer As Outlook.Explorer = Nothing

    Private Sub ThisAddIn_Startup(ByVal sender As Object, _
         ByVal e As System.EventArgs) Handles Me.Startup
        currentExplorer = Me.Application.Explorers.Application.ActiveExplorer
        AddHandler currentExplorer.SelectionChange, AddressOf _
    End Sub

    Public Sub currentExplorer_Event()
        Dim selectedFolder As Outlook.MAPIFolder = _
        Dim expMessage As String = "Your current folder is " _
            & selectedFolder.Name & "." & vbCrLf
        Dim itemMessage As String = "Item is unknown."
            If Me.Application.ActiveExplorer.Selection.Count > 0 Then
                Dim selObject As Object = Me.Application.ActiveExplorer _
                If (TypeOf selObject Is Outlook.MailItem) Then
                    Dim mailItem As Outlook.MailItem = _
                        TryCast(selObject, Outlook.MailItem)
                    itemMessage = "The item is an e-mail message." & _
                        " The subject is " & mailItem.Subject & "."
                ElseIf (TypeOf selObject Is Outlook.ContactItem) Then
                    Dim contactItem As Outlook.ContactItem = _
                       TryCast(selObject, Outlook.ContactItem)
                    itemMessage = "The item is a contact." & _
                        " The full name is " & _
                        contactItem.Subject & "."
                ElseIf (TypeOf selObject Is Outlook. _
                    AppointmentItem) Then
                    Dim apptItem As Outlook.AppointmentItem = _
                       TryCast(selObject, Outlook.AppointmentItem)
                    itemMessage = "The item is an appointment." _
                        & apptItem.Subject & "."
                ElseIf (TypeOf selObject Is Outlook.TaskItem) Then
                    Dim taskItem As Outlook.TaskItem = _
                        TryCast(selObject, Outlook.TaskItem)
                    itemMessage = "The item is a task." & _
                        " The body is " & taskItem.Body & "."
                ElseIf (TypeOf selObject Is Outlook.MeetingItem) Then
                    Dim meetingItem As Outlook.MeetingItem = _
                        TryCast(selObject, Outlook.MeetingItem)
                    itemMessage = "The item is a meeting item. " & _
                        "The subject is " & meetingItem.Subject & "."
                End If
            End If
            expMessage = expMessage & itemMessage
        Catch ex As Exception
            expMessage = ex.Message
        End Try
    End Sub

This example requires:

  • Appointment, contact, and e-mail items in Microsoft Office Outlook.

Outlook Object Model Overview
How to: Programmatically Retrieve a Folder by Name
How to: Programmatically Search for a Specific Contact