web 2.0


Importing Outlook's holiday list into a function in Excel

This quick articles will show you how to retrieve the holidays stored in Outlook and return them in a function in Excel. This article assumes that the holidays have been imported into Outlook.

To import the holidays in Outlook you should follow this instruction: Tools à Options à Preferences à Calendar Options. Then, click on Add Holidays and choose the country for which you want the holidays. You can choose as many as you want.

Another note is that the code will work in English, but will not function in other languages. You may adapt it to your our language, but changing the filter. This happens because some of the fields in the calendar have been translated into local languages whereas others have not. So you will find that the field Categories is Categorias in Spanish, for example.

To start with, add a new class module to your project. Then go to Tool à References à Microsoft Outlook xx Object Library and install this library. We will use Outlook’s object library to access some of its resources and facilitate the programming process.

Rename the class you added to clsHolidays and open it, where you should add the following lines of code:

Function Holidays(ByVal StartsOn As String, ByVal EndsOn As String, _
    Location As String) As Variant

    Dim appOutlook      As Outlook.Application
    Dim nSpace          As Outlook.Namespace
    Dim calFolder       As Outlook.MAPIFolder
    Dim calItem         As Outlook.AppointmentItem
    Dim filterItems     As Outlook.Items
    Dim strFilter       As
String
    Dim holName         As New Collection
    Dim holDate         As New Collection
    Dim i               As
Integer
    Dim aHoliday   

    Set appOutlook = CreateObject("Outlook.Application")
    Set nSpace = appOutlook.GetNamespace("MAPI")
    Set calFolder = nSpace.GetDefaultFolder(olFolderCalendar) 

    strFilter = "[Categories]= 'Holiday' And [Location]= '"
    strFilter = strFilter & Location & "'" & " And [Start]>= '"
    strFilter = strFilter & StartsOn & "'" & " And [End]<= '"
    strFilter = strFilter & EndsOn & "'"

    Set filterItems = calFolder.Items.Restrict(strFilter)
    filterItems.Sort "[Start]", False   

    On Error Resume Next
    For Each calItem In filterItems
        holName.Add calItem.Subject, calItem.Subject
        holDate.Add calItem.Start, CStr(calItem.Start)
    Next   

    ReDim aHoliday(holName.Count - 1, 1)   

    For i = 0 To holName.Count - 1
        aHoliday(i, 0) = holName.Item(i + 1)
        aHoliday(i, 1) = holDate.Item(i + 1)
    Next   

    Holidays = aHoliday   

    Set appOutlook = Nothing
    Set nSpace =
Nothing
    Set calFolder =
Nothing
    Set calItem =
Nothing
    Set filterItems =
Nothing
    Set holName =
Nothing
    Set holDate = Nothing 

End Function

With the code within the module, you should now add a module. In this module, you will enter de following lines of code:

Function Holidays(InitialDate As String, EndDate As String, _
        Country As String) As
Variant
    Dim Holiday As New clsHolidays
    Holidays = Holiday.Holidays(InitialDate, EndDate, Country)

End
Function

This will return an array function and, therefore, you should enter it using CTRL+SHIFT+ENTER. The dates I chose to be entered as strings rather than dates. However, you may change that if you wish.

You can now test the function in your spreadsheet:


Picture 1 - Holidays from Outlook's calendar

Tags: , , ,

Microsoft Outlook | Microsoft Outlook - VBA

Comments are closed