Identify and Get Selected Item using TypeItem in Outlook using VBA

Identify and Get Selected Item using TypeItem in Outlook using VBA

TypeItem

Method offers capability to return what type of object you are dealing with in outlook. In this article, I will be writing code to identify what type of item user has selected in outlook. Ideally the items in outlook are classified as:

  1. Explorer
  2. Inspector

Code example

Public Function GetCurrentItem() As Object
    'Outlook application object declaration
    Dim oApp As Outlook.Application
    
    'bind current outlook instance
    Set oApp = GetObject("", "Outlook.Application")
    
    Select Case TypeName(oApp.ActiveWindow)
        Case "Explorer"
            Set GetCurrentItem = oApp.ActiveExplorer.Selection.Item(1)
        Case "Inspector"
            Set GetCurrentItem = oApp.ActiveInspector.CurrentItem
    End Select
    
    'Cleanup
    If Not oApp Is Nothing Then
        Set oApp = Nothing
    End If
End Function

Calling method

Public Sub GetItemType()
    Dim oItem As Object
    Set oItem = GetCurrentItem
    If oItem.Class = olAppointment Then
        Debug.Print "You have Appointment window opened"
    ElseIf oItem.Class = olMail Then
        Debug.Print "You have Mail window opened"
    End If
End Sub

Input

Output

You have Mail window opened

Next >> Create Outlook Reminder task using VBA code

Leave a Reply

Your email address will not be published.