Eugene Astafiev

How To: Get any standard / default folder in Outlook

    One of the most common tasks in Outlook programming a developer may face is getting a standard folder. Here I will show you how to get it. The Outlook Object Model provides the GetDefaultFolder function of the Namespace class for this task.
    The GetDefaultFolder function accepts the OlDefaultFolders enumeration and returns a standard Outlook Folder we are interested in (for the current profile of course). If, for any reason, the folder of the requested type doesn’t exist, GetDefaultFolder returns null (Nothing in VB.NET).
    The code you can see below demonstrates how to get the Inbox folder and then display its name in the message box. To get the code running, you need to pass an instance of the Outlook Application class to the GetDefaultFolder method. In Add-in Express based add-ins you can retrieve it using the OutlookApp property of the add-in module and in VSTO based add-ins please use the Application property of the add-in class.
    C# and Add-in Express:
private void GetDefaultFolder(Outlook._Application OutlookApp) 
{ 
    Outlook.NameSpace nameSpace = OutlookApp.GetNamespace("MAPI"); 
    Outlook.MAPIFolder folderInbox = nameSpace.GetDefaultFolder(
        Outlook.OlDefaultFolders.olFolderInbox); 
        // See also:
        // olFolderCalendar
        // olFolderConflicts 
        // olFolderContacts 
        // olFolderDeletedItems 
        // olFolderDrafts 
        // olFolderInbox 
        // olFolderJournal 
        // olFolderJunk 
        // olFolderLocalFailures 
        // olFolderManagedEmail 
        // olFolderNotes 
        // olFolderOutbox 
        // olFolderSentMail 
        // olFolderServerFailures 
        // olFolderSuggestedContacts 
        // olFolderSyncIssues 
        // olFolderTasks 
        // olFolderToDo 
        // olPublicFoldersAllPublicFolders 
        // olFolderRssFeeds 
    if (folderInbox != null) // Inbox 
    {
       System.Windows.Forms.MessageBox.Show(folderInbox.Name); 
       Marshal.ReleaseComObject(folderInbox); 
    }
    if (nameSpace != null) Marshal.ReleaseComObject(nameSpace); 
}
    VB.NET and Add-in Express:
Private Sub GetDefaultFolder(OutlookApp As Outlook._Application) 
    Dim olNameSpace As Outlook.NameSpace = OutlookApp.GetNamespace("MAPI") 
    Dim folderInbox As Outlook.MAPIFolder = olNameSpace.GetDefaultFolder( _ 
        Outlook.OlDefaultFolders.olFolderInbox) 
        ' See also:
        ' olFolderCalendar
        ' olFolderConflicts 
        ' olFolderContacts 
        ' olFolderDeletedItems 
        ' olFolderDrafts 
        ' olFolderInbox 
        ' olFolderJournal 
        ' olFolderJunk 
        ' olFolderLocalFailures 
        ' olFolderManagedEmail 
        ' olFolderNotes 
        ' olFolderOutbox 
        ' olFolderSentMail 
        ' olFolderServerFailures 
        ' olFolderSuggestedContacts 
        ' olFolderSyncIssues 
        ' olFolderTasks 
        ' olFolderToDo 
        ' olPublicFoldersAllPublicFolders 
        ' olFolderRssFeeds 
    If Not IsNothing(folderInbox) Then ' Inbox 
       System.Windows.Forms.MessageBox.Show(folderInbox.Name) 
       Marshal.ReleaseComObject(folderInbox) 
    End If 
    If Not IsNothing(olNameSpace) Then Marshal.ReleaseComObject(olNameSpace) 
End Sub
    C# and VSTO:
using System.Runtime.InteropServices; 
// … 
private void GetDefaultFolder(Outlook._Application Application)
{ 
    Outlook.NameSpace nameSpace = Application.GetNamespace("MAPI"); 
    Outlook.MAPIFolder folderInbox = nameSpace.GetDefaultFolder(
       Outlook.OlDefaultFolders.olFolderInbox); 
       // See also:
       // olFolderCalendar
       // olFolderConflicts 
       // olFolderContacts 
       // olFolderDeletedItems 
       // olFolderDrafts 
       // olFolderInbox 
       // olFolderJournal 
       // olFolderJunk 
       // olFolderLocalFailures 
       // olFolderManagedEmail 
       // olFolderNotes 
       // olFolderOutbox 
       // olFolderSentMail 
       // olFolderServerFailures 
       // olFolderSuggestedContacts 
       // olFolderSyncIssues 
       // olFolderTasks 
       // olFolderToDo 
       // olPublicFoldersAllPublicFolders 
       // olFolderRssFeeds 
    if (folderInbox != null) // Inbox 
    {
       System.Windows.Forms.MessageBox.Show(folderInbox.Name); 
       Marshal.ReleaseComObject(folderInbox); 
    }
    if (nameSpace != null) Marshal.ReleaseComObject(nameSpace); 
}
    VB.NET and VSTO:
Imports System.Runtime.InteropServices 
' … 
Private Sub GetDefaultFolder(Application As Outlook.Application) 
   Dim olNameSpace As Outlook.NameSpace = Application.GetNamespace("MAPI") 
   Dim folderInbox As Outlook.MAPIFolder = olNameSpace.GetDefaultFolder( _
       Outlook.OlDefaultFolders.olFolderInbox)
       ' See also:
       ' olFolderCalendar
       ' olFolderConflicts 
       ' olFolderContacts 
       ' olFolderDeletedItems 
       ' olFolderDrafts 
       ' olFolderInbox 
       ' olFolderJournal 
       ' olFolderJunk 
       ' olFolderLocalFailures 
       ' olFolderManagedEmail 
       ' olFolderNotes 
       ' olFolderOutbox 
       ' olFolderSentMail 
       ' olFolderServerFailures 
       ' olFolderSuggestedContacts 
       ' olFolderSyncIssues 
       ' olFolderTasks 
       ' olFolderToDo 
       ' olPublicFoldersAllPublicFolders 
       ' olFolderRssFeeds 
   If Not IsNothing(folderInbox) Then ' Inbox 
      System.Windows.Forms.MessageBox.Show(folderInbox.Name) 
      Marshal.ReleaseComObject(folderInbox)
   End If 
   If Not IsNothing(olNameSpace) Then Marshal.ReleaseComObject(olNameSpace) 
End Sub
    I would also like to draw your attention to the GetSharedDefaultFolder function of the Namespace class in Outlook. This function allows getting a default folder for the specified user. In this case one user should delegate access to one or more of their default folders to another user.
    The GetSharedDefaultFolder function accepts two parameters: the first is an instance of the Recipient class (it must be resolved before you use it in the GetSharedDefaultFolder function) and the second one is the OlDefaultFolders enumeration. The following values are possible for the second parameter: olFolderCalendar, olFolderContacts, olFolderDrafts, olFolderInbox, olFolderJournal, olFolderNotes and olFolderTasks.
    The code below shows the way of getting a shared calendar for the user and display the shared calendar folder for the user (I used my name for demonstration purposes).
    C# and Add-in Express:
private void GetSharedDefaultFolder(Outlook._Application OutlookApp) 
{
   Outlook.MAPIFolder olSharedCalendarFolder = null; 
   Outlook.NameSpace nameSpace = OutlookApp.GetNamespace("MAPI"); 
   Outlook.Recipient myRecipient = nameSpace.CreateRecipient("Eugene Astafiev"); 
   myRecipient.Resolve(); 
   if (myRecipient.Resolved) 
   { 
      olSharedCalendarFolder = nameSpace.GetSharedDefaultFolder( _
          myRecipient, Outlook.OlDefaultFolders.olFolderCalendar); 
                       // See also:
                       // olFolderCalendar
                       // olFolderContacts
                       // olFolderDrafts
                       // olFolderInbox
                       // olFolderJournal
                       // olFolderNotes
                       // olFolderTasks
      if (olSharedCalendarFolder != null) 
      {
          olSharedCalendarFolder.Display(); 
          Marshal.ReleaseComObject(olSharedCalendarFolder); 
      }
   } 
   if (myRecipient != null) Marshal.ReleaseComObject(myRecipient); 
   if (nameSpace != null) Marshal.ReleaseComObject(nameSpace); 
}
    VB.NET and Add-in Express:
Private Sub GetSharedDefaultFolder(OutlookApp As Outlook._Application) 
    Dim olSharedCalendarFolder As Outlook.MAPIFolder = Nothing 
    Dim olNameSpace As Outlook.NameSpace = OutlookApp.GetNamespace("MAPI") 
    Dim myRecipient As Outlook.Recipient = olNameSpace.CreateRecipient("Eugene Astafiev") 
    myRecipient.Resolve() 
    If myRecipient.Resolved Then 
       olSharedCalendarFolder = olNameSpace.GetSharedDefaultFolder( _
           myRecipient, Outlook.OlDefaultFolders.olFolderCalendar) 
                        ' See also:
                        ' olFolderCalendar
                        ' olFolderContacts
                        ' olFolderDrafts
                        ' olFolderInbox
                        ' olFolderJournal
                        ' olFolderNotes
                        ' olFolderTasks
    If Not IsNothing(olSharedCalendarFolder) Then 
       olSharedCalendarFolder.Display() 
       Then Marshal.ReleaseComObject(olSharedCalendarFolder) 
    End If 
    If Not IsNothing(myRecipient) Then Marshal.ReleaseComObject(myRecipient) 
    If Not IsNothing(olNameSpace) Then Marshal.ReleaseComObject(olNameSpace) 
End Sub
    C# and VSTO:
using System.Runtime.InteropServices; 
// … 
private void GetSharedDefaultFolder(Outlook._Application OutlookApp) 
{ 
    Outlook.MAPIFolder olSharedCalendarFolder = null; 
    Outlook.NameSpace nameSpace = OutlookApp.GetNamespace("MAPI"); 
    Outlook.Recipient myRecipient = nameSpace.CreateRecipient("Eugene Astafiev"); 
    myRecipient.Resolve(); 
    if (myRecipient.Resolved) 
    { 
        olSharedCalendarFolder = nameSpace.GetSharedDefaultFolder(
            myRecipient, Outlook.OlDefaultFolders.olFolderCalendar); 
                         // See also:
                         // olFolderCalendar
                         // olFolderContacts
                         // olFolderDrafts
                         // olFolderInbox
                         // olFolderJournal
                         // olFolderNotes
                         // olFolderTasks
        if (olSharedCalendarFolder != null) 
        {
           olSharedCalendarFolder.Display(); 
           Marshal.ReleaseComObject(olSharedCalendarFolder);
        }
    } 
    if (myRecipient != null) Marshal.ReleaseComObject(myRecipient); 
    if (nameSpace != null) Marshal.ReleaseComObject(nameSpace); }
    VB.NET and VSTO:
Imports System.Runtime.InteropServices 
' … 
Private Sub GetSharedDefaultFolder(OutlookApp As Outlook._Application) 
    Dim olSharedCalendarFolder As Outlook.MAPIFolder = Nothing 
    Dim olNameSpace As Outlook.NameSpace = OutlookApp.GetNamespace("MAPI") 
    Dim myRecipient As Outlook.Recipient = olNameSpace.CreateRecipient("Eugene Astafiev") 
    myRecipient.Resolve() 
    If myRecipient.Resolved Then 
       olSharedCalendarFolder = olNameSpace.GetSharedDefaultFolder( _
            myRecipient, Outlook.OlDefaultFolders.olFolderCalendar) 
                         ' See also:
                         ' olFolderCalendar
                         ' olFolderContacts
                         ' olFolderDrafts
                         ' olFolderInbox
                         ' olFolderJournal
                         ' olFolderNotes
                         ' olFolderTasks
       If Not IsNothing(olSharedCalendarFolder) Then
          olSharedCalendarFolder.Display() 
          Marshal.ReleaseComObject(olSharedCalendarFolder) 
       End If
    End If 
    If Not IsNothing(myRecipient) Then Marshal.ReleaseComObject(myRecipient) 
    If Not IsNothing(olNameSpace) Then Marshal.ReleaseComObject(olNameSpace) 
End Sub
    Note that Outlook 2007 and 2010 introduce some new classes to developers. Among others is the Store class. It provides developers with the following useful functions:
  1. GetDefaultFolder (Outlook 2010) – returns the default folder (specified by the argument) in the store.
  2. GetRootFolder (Outlook 2007) – returns a folder representing the root-level folder of the store.
  3. GetSearchFolders (Outlook 2007) – returns a Folders collection representing the search folders for the store.
  4. GetSpecialFolder (Outlook 2007) – returns the folder object for a special folder (specified by the argument).

6 Comments

  • Marzio Morandi says:

    Hi,

    olFolderDeletedItems return Eroori in Outlook 2013

    Marzio

  • Andrei Smolin (Add-in Express Team) says:

    Hello Mario,

    What error are you getting?

  • Aniket says:

    I need default folder for all profiles configured, unlike the current profile only mentioned in above code. As I wish to handle an event whenever new item is added to any of the inbox.

  • Andrei Smolin (Add-in Express Team) says:

    Hello Aniket,

    Get {Outlook.Application object}.Session.Accounts. For each Account object, get {an Account object}.DeliveryStore, and finally, call (an Outlook.Store object).GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox). See also https://msdn.microsoft.com/en-us/VBA/outlook-vba/articles/store-getdefaultfolder-method-outlook. As to getting an event when an email is delivered to such a folder, see https://www.add-in-express.com/creating-addins-blog/outlook-newmail-custom-solution/.

  • Hannah Bo says:

    Hi Expert,

    I can create an appointment on abc’s calendar using below code.

    Outlook.Recipient myRecipient = nameSpace.CreateRecipient(“abc”);
    myRecipient.Resolve();
    if (myRecipient.Resolved)
    {
    olSharedCalendarFolder = nameSpace.GetSharedDefaultFolder(
    myRecipient, Outlook.OlDefaultFolders.olFolderCalendar);
    var items = olfld.Items;
    aitem = items.Add(Outlook.OlItemType.olAppointmentItem)as Outlook.AppointmentItem;
    }

    The problem is that how can I know the calendar I have clicked on is for delegate user “abc”. Like Skype add-in it will generate the skype url for abc when I click on abc’s calendar and click “new skype meeting”, I don’t know how to get the name/address of the calendar I have currently selected, which needs to be passed to nameSpace.CreateRecipient() in above code.

    From CalendarView I can get the properties like selected start time and end time, but I don’t know which property indicates the owner..

    Thanks ahead!

  • Dmitry Kostochko (Add-in Express Team) says:

    Hello Hannah,

    As far as I understand, you have access to the AppointmentItem object, correct? If so, try to use the GetOrganizer() method, hope it will help.

Post a comment

Have any questions? Ask us right now!