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 meSpace = = Application.GetNamespace("MAPI"); 
    Outlook.MAPIFolder folderInbox = nameSpace.GetDefaultFolder(
       Outlook.OlDefaultFolders.olFolderInbox); 
     // See also:
:
     // olFolderCalendar
r
     // 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 S
    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 meSpace = = 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
r
                       // olFolderContacts
s
                       // olFolderDrafts
s
                       // olFolderInbox
x
                       // olFolderJournal
l
                       // olFolderNotes
s
                       // olFolderTasks
s
        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 S
    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).

2 Comments

Post a comment

Have any questions? Ask us right now!