Filter Meetings in Outlook Add-in using Visual Basic for Applications

Filter Meetings in Outlook Add-in using Visual Basic for Applications

Filter

If you need to filter meetings based on date, company name, subject etc, Outlook VBA offers Restrict method which takes filter criteria and provide calendar items as a result which you can take for further manipulation as given in below code. Where we are iterating all the appointments or calendar tasks for specific date.

Code example

Public Sub GetAllMeetings()
    'Outlook application object declaration
    Dim oApp As Outlook.Application
    
    'bind current outlook instance
    Set oApp = GetObject("", "Outlook.Application")
    
    'Bind namespace
    Dim oNameSpace As Outlook.Namespace
    Set oNameSpace = oApp.GetNamespace("MAPI")
    
    Dim oFolder As Folder
    Set oFolder = oNameSpace.GetDefaultFolder(olFolderTasks)
    
    Dim oCriteria As String
    oCriteria = "[DueDate] > '" & Format(Now, "mm/dd/yyyy") & "'"
    
    Dim oAllItems As Items
    Set oAllItems = oFolder.Items.Restrict(oCriteria)
    
    Dim oAppointment As TaskItem
    For Each oAppointment In oAllItems
        If oAppointment.Class = olTask Then
            Debug.Print oAppointment.Subject
            Debug.Print oAppointment.EntryID
        End If
    Next oAppointment
    
    'CleanUp
    If Not oAppointment Is Nothing Then
        Set oAppointment = Nothing
    End If
    If Not oAllItems Is Nothing Then
        Set oAllItems = Nothing
    End If
    If Not oFolder Is Nothing Then
        Set oFolder = Nothing
    End If
    If Not oNameSpace Is Nothing Then
        Set oNameSpace = Nothing
    End If
    If Not oApp Is Nothing Then
        Set oApp = Nothing
    End If
End Sub

Explanation

Above code we are holding all tasks in a folder object which we use to filter items based on oCriteria variable where [DueDate] >= today’s date with the help of Restrict method. Next we are iterating each item through loop and extracting various properties of each contact.

Next >> Share Calendar (.ics) in Outlook using VBA code example

Leave a Reply

Your email address will not be published.