Utility Functions for use with Outlook 2003 VBA

The following is a series of utility functions for Outlook VBA to simplify some of the most common operations. You may have been directed here from another page, because just like a VBA project should not have multiple copies of the same function, neither should a website.

Item Typenames

Determine if a given item is a specific type using these functions.

Function IsTask(itm As Object) As Boolean
  IsTask = (TypeName(itm) = "TaskItem")
End Function
Function IsContact(itm As Object) As Boolean
  IsContact = (TypeName(itm) = "ContactItem")
End Function
Function IsMail(itm As Object) As Boolean
  IsMail = (TypeName(itm) = "MailItem")
End Function
Function IsDistributionList(itm As Object) As Boolean
  IsDistributionList = (TypeName(itm) = "DistListItem")
End Function
Function IsDocumentItem(itm As Object) As Boolean
  IsDocumentItem = (TypeName(itm) = "DocumentItem")
End Function
Function IsMeeting(itm As Object) As Boolean
  IsMeeting = (TypeName(itm) = "AppointmentItem" And _
    itm.MeetingStatus = olMeeting)
End Function
Function IsAppointment(itm As Object) As Boolean
  IsAppointment = (TypeName(itm) = "AppointmentItem" And _
    itm.MeetingStatus = olNonMeeting)
End Function
Function IsNote(itm As Object) As Boolean
  IsNote = (TypeName(itm) = "NoteItem")
End Function

Grab the Recipients Collection for any object

This function returns the Recipients Collection for any object that supports it.

Function GetRecipients(itm As Object) As Outlook.Recipients
  Dim obj As Object
  Dim recips As Outlook.Recipients
  Dim types() As String
  types = Split("MailItem,AppointmentItem,JournalItem,MeetingItem,TaskItem", ",")
  If UBound(Filter(types, TypeName(itm))) > -1 Then  ' it's a matching item
    Set obj = itm
    Set recips = obj.Recipients
    Set GetRecipients = recips
  End If
End Function

Explorers, Inspectors and Folders

Determine if a given item is an Explorer, Inspector or Folder using these functions.

Function IsExplorer(itm As Object) As Boolean
  IsExplorer = (TypeName(itm) = "Explorer")
End Function
Function IsInspector(itm As Object) As Boolean
  IsInspector = (TypeName(itm) = "Inspector")
End Function
Function IsFolder(itm As Object) As Boolean
  IsFolder = (TypeName(itm) = "MAPIFolder") ' use "Folder" for Outlook 2007+
End Function

Return the current or selected email message

This function will grab the currently open email, or the selected email in the Explorer window if no email is open.

Function GetMailItem() As Outlook.MailItem
' note that this requires the IsExplorer and IsInspector functions found elsewhere
  Dim obj As Object

  Select Case True
  Case IsExplorer(Application.ActiveWindow)
    Set obj = ActiveExplorer.Selection.Item(1)
  Case IsInspector(Application.ActiveWindow)
    Set obj = ActiveInspector.CurrentItem
  End Select
  If TypeName(obj) = "MailItem" Then
    Set GetMailItem = obj
  End If
End Function

Return the currently selected item

This function returns the currently open item, or the one selected in the Explorer window if no item is open.

Function GetCurrentItem() As Object
' note that this requires the IsExplorer and IsInspector functions found elsewhere
  Dim obj As Object

  Select Case True
  Case IsExplorer(Application.ActiveWindow)
    Set obj = ActiveExplorer.Selection.Item(1)
  Case IsInspector(Application.ActiveWindow)
    Set obj = ActiveInspector.CurrentItem
  End Select
  Set GetCurrentItem = obj
End Function

Return the native Outlook.Application object

Function GetOutlookApp() As Outlook.Application
' returns reference to native Application object
  Set GetOutlookApp = Outlook.Application
End Function

Returning the current NameSpace Object

Function GetNS(ByRef app As Outlook.Application) _
    As Outlook.NameSpace
  Set GetNS = app.GetNamespace("MAPI")
End Function

Create and return a new item

Function GetOutlookItem(ByRef olApp As Outlook.Application, _
                        whatItem As OlItemType) As Object
  Set GetOutlookItem = olApp.CreateItem(whatItem)
End Function

Get the Items collection for any default folder

Function GetItems(olNS As Outlook.NameSpace, _
    folder As OlDefaultFolders) As Outlook.Items
  Set GetItems = olNS.GetDefaultFolder(folder).Items
End Function

Default Item Type Folders

These functions will tell you if a given folder is of a specific type. Change MAPIFolder to Folder for Outlook 2007+.

Function IsMailFolder(fldr As Outlook.MAPIFolder) As Boolean
  IsMailFolder = (fldr.DefaultItemType = olMailItem)
End Function
Function IsApptFolder(fldr As Outlook.MAPIFolder) As Boolean
  IsApptFolder = (fldr.DefaultItemType = olAppointmentItem)
End Function
Function IsTaskFolder(fldr As Outlook.MAPIFolder) As Boolean
  IsTaskFolder = (fldr.DefaultItemType = olTaskItem)
End Function
Function IsContactFolder(fldr As Outlook.MAPIFolder) As Boolean
  IsContactFolder = (fldr.DefaultItemType = olContactItem)
End Function
Function IsJournalFolder(fldr As Outlook.MAPIFolder) As Boolean
  IsJournalFolder = (fldr.DefaultItemType = olJournalItem)
End Function
Function IsNoteFolder(fldr As Outlook.MAPIFolder) As Boolean
  IsNoteFolder = (fldr.DefaultItemType = olNoteItem)
End Function

These functions can be stacked as follows:

Dim Itms As Outlook.Items
Set Itms = GetItems(GetNS(GetOutlookApp), olFolderInbox)

Site last updated: May 17, 2012

Excel School