Link to home
Start Free TrialLog in
Avatar of CAGreenlaw
CAGreenlaw

asked on

Open a non-default shared calendar in Oulook using VBA

I'm trying to open a non-default shared calendar in Outlook using VBA but I only know how to open the default one. I have spent a considerable amount of time researching this online but no luck. Any help would be greatly appreciated.

The user name of the shared calendar is "John Burns" and the non-default calendar name is "Van".

My code to retrieve the default shared calendar is :

    Dim myOlApp As Outlook.Application
    Dim myNms As Outlook.NameSpace
    Dim myFolder As Outlook.MAPIFolder
    Dim myRecipient As Outlook.Recipient
    Dim myExplorer As Outlook.Explorer
    Dim SharedFolder As Outlook.MAPIFolder
   
    Set myOlApp = CreateObject("Outlook.Application")    
    Set myNms = myOlApp.GetNamespace("MAPI")
    Set myFolder = myNms.GetDefaultFolder(olFolderCalendar)
    Set myExplorer = myOlApp.ActiveExplorer
    Set myExplorer.CurrentFolder = myFolder
   
    Set myRecipient = myNms.CreateRecipient("John Burns")
    Set SharedFolder = myNms.GetSharedDefaultFolder(myRecipient, olFolderCalendar)
   
    myExplorer.SelectFolder SharedFolder
Avatar of David Lee
David Lee
Flag of United States of America image

Hi, CAGreenlaw.

You can use the following function to open any Outlook folder, default or otherwise.  Here's how to call it

Set myFolder = OpenOutlookFolder("Path to the folder you want to open")

Function OpenOutlookFolder(strFolderPath)
    ' Purpose: Opens an Outlook folder from a folder path.'
    ' Written: 4/24/2009'
    ' Author:  BlueDevilFan'
    ' Outlook: All versions'
    Dim arrFolders, varFolder, bolBeyondRoot
    On Error Resume Next
    If strFolderPath = "" Then
        Set OpenOutlookFolder = Nothing
    Else
        Do While Left(strFolderPath, 1) = "\"
            strFolderPath = Right(strFolderPath, Len(strFolderPath) - 1)
        Loop
        arrFolders = Split(strFolderPath, "\")
        For Each varFolder In arrFolders
            Select Case bolBeyondRoot
                Case False
                    Set OpenOutlookFolder = olkSes.Folders(varFolder)
                    bolBeyondRoot = True
                Case True
                    Set OpenOutlookFolder = OpenOutlookFolder.Folders(varFolder)
            End Select
            If Err.Number <> 0 Then
                Set OpenOutlookFolder = Nothing
                Exit For
            End If
        Next
    End If
    On Error GoTo 0
End Function

Open in new window

Avatar of CAGreenlaw
CAGreenlaw

ASKER

Thanks so much for your quick response BlueDevilFan. I'm not sure how to make this code work for me though (ie. How do I determine the path to the other user's calendar?). I'm trying to open a shared non-default calendar that hasn't already been added to my profile. I think I need to utilize the GetSharedDefaultFolder method but it only returns the default calendar from the user. The other user has several calendars and I need to pick the one called Van and open it. Any ideas?
ASKER CERTIFIED SOLUTION
Avatar of David Lee
David Lee
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Wow, you really know your stuff BlueDevilFan. Thank you so much, it worked like a charm!
You're welcome, CAGreenlaw.  Glad I could help out.