Obtaining Folder Objects from Folder Paths in Outlook 2007
This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.
Summary: Create an add-in for Microsoft Office Outlook 2007 to navigate Outlook folders and find folders that contain specified data.
Applies to: 2007 Microsoft Office system, Microsoft Office Outlook 2007, Microsoft Visual Studio 2005, Microsoft Visual Studio 2005 Tools for the 2007 Microsoft Office System Second Edition
Ken Getz, MCW Technologies, LLC
September 2007
Code It | Read It | Explore It
Code It
Imagine that in your organization, you know that all users have a folder in their Inbox folder named Customers, and in the Customers folder is a folder named Archive. You want to navigate to that folder, and if it exists, display it in a new window.
In this example, you create a Microsoft Office Fluent Ribbon customization that adds a button that is available when you are viewing a contact. By clicking the button, you can navigate to the Inbox\Customers\Archive folder and display the folder contents, if it exists. This particular location for the button is arbitrary, but it allows you to test the sample code. To demonstrate the technique, follow these steps.
To create Ribbon customization that adds a button
In Microsoft Visual Studio 2005, create a Visual Studio 2005 Second Edition add-in for Microsoft Office Outlook 2007. Name the new project ObtainFolderObjectAddIn.
In Solution Explorer, right-click the project.
On the shortcut menu, click Add, and then click New Item.
In the Add New Item dialog box, click Ribbon support.
Click Add to insert the new Ribbon customization.
Modify the new Ribbon1.xml file, replacing the existing content with the following XML.
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" onLoad="OnLoad"> <ribbon> <tabs> <tab idMso="TabAddIns"> <group id="MyGroup" label="How To"> <button id="button1" label="Display Customers" screentip="Switch to Inbox\Customers Folder" onAction="OnClick" /> </group> </tab> </tabs> </ribbon> </customUI>
In Solution Explorer, right-click the ObtainFolderObject project.
On the shortcut menu, click Properties.
In the Properties pane, click the Resources tab.
In Solution Explorer, drag the Ribbon1.xml file into the Properties pane.
This action adds the Ribbon1.xml file as a project resource.
Close the Properties pane, clicking Yes when prompted to save.
In the Ribbon1.vb or Ribbon1.cs file, uncomment the ThisAddIn partial class.
In Microsoft Visual C# only, at the top of the code file, add the following using statement.
using Outlook=Microsoft.Office.Interop.Outlook;
In the Ribbon1 class, modify the existing GetCustomUI implementation so that it retrieves the Ribbon1 resource, and returns only the resource if the current ribbonID value is Microsoft.Outlook.Contact. In Visual C#, you must expand the IRibbonExtensibility members code region to find the procedure.
Public Function GetCustomUI(ByVal ribbonID As String) As String _ Implements Office.IRibbonExtensibility.GetCustomUI If ribbonID = "Microsoft.Outlook.Contact" Then Return My.Resources.Ribbon1 Else Return String.Empty End If End Function
public string GetCustomUI(string ribbonID) { if (ribbonID == "Microsoft.Outlook.Contact") { return Properties.Resources.Ribbon1; } else { return String.Empty; } }
In the Ribbon1 class, add the following declaration, making it simpler to refer to the Outlook Application object.
Private Application As Outlook.Application = _ Globals.ThisAddIn.Application
private Outlook.Application Application = Globals.ThisAddIn.Application;
In the Ribbon1 class, add the following callback procedure. Clicking the new button on the Ribbon calls this procedure.
Public Sub OnClick(ByVal control As Office.IRibbonControl) End Sub
public void OnClick(Office.IRibbonControl control) { }
Save the project, and run it. In Outlook 2007, create a contact. On the Ribbon, click the Add-Ins tab, and verify that you see the new button on the ribbon. Close Outlook when you finish, returning to Visual Studio 2005.
Run Outlook 2007, and browse to your Inbox folder.
If it does not already exist, add a new subfolder named Customers.
In the Customers folder, create a subfolder named Archive. If you like, copy some e-mail messages into this folder.
Quit Outlook 2007 when you finish.
In the Ribbon1 class, add the following procedure.
Private Function GetFolder( _ ByVal folderPath As String) As Outlook.Folder Dim returnFolder As Outlook.Folder = Nothing Try ' Remove leading "\" characters. folderPath = folderPath.TrimStart("\".ToCharArray()) ' Split the folder path into individual folder names. Dim folders As String() = folderPath.Split("\".ToCharArray()) ' Retrieve a reference to the root folder. returnFolder = _ TryCast(Application.Session.Folders(folders(0)), _ Outlook.Folder) ' If the root folder exists, look in subfolders. If returnFolder IsNot Nothing Then Dim subFolders As Outlook.Folders = Nothing Dim folderName As String ' Look through folder names, skipping the first folder, ' which you already retrieved. For i As Integer = 1 To folders.Length - 1 folderName = folders(i) subFolders = returnFolder.Folders returnFolder = _ TryCast(subFolders(folderName), Outlook.Folder) Next End If Catch ' Do nothing at all -- just return a null reference. returnFolder = Nothing End Try Return returnFolder End Function
private Outlook.Folder GetFolder(string folderPath) { Outlook.Folder returnFolder = null; try { // Remove leading "\" characters. folderPath = folderPath.TrimStart("\\".ToCharArray()); // Split the folder path into individual folder names. String[] folders = folderPath.Split("\\".ToCharArray()); // Retrieve a reference to the root folder. returnFolder = Application.Session.Folders[folders[0]] as Outlook.Folder; // If the root folder exists, look in subfolders. if (returnFolder != null) { Outlook.Folders subFolders = null; String folderName; // Look through folder names, skipping the first // folder, which you already retrieved. for (int i = 1; i < folders.Length; i++) { folderName = folders[i]; subFolders = returnFolder.Folders; returnFolder = subFolders[folderName] as Outlook.Folder; } } } catch { // Do nothing at all -- just return a null reference. returnFolder = null; } return returnFolder; }
This procedure takes the full path you send, in the form Mailbox—Your Name\Inbox\Customers\Archive, and splits the path into individual folder names. It attempts to locate the root folder, Mailbox—Your Name, and, if it succeeds, loops through all the remaining folder names, each time retrieving a reference to the corresponding folder, until it runs out of names. If it successfully reaches the last folder name, it returns the corresponding Outlook.Folder object.
To modify the existing OnClick procedure
Modify the existing OnClick procedure, so that it retrieves a reference to a root folder, passes the full path to the GetFolder procedure, and then displays the selected folder, if it is found.
Public Sub OnClick(ByVal control As Office.IRibbonControl) ' Retrieve the name of the top-level folder, for ' the purposes of this demonstration. Dim inbox As Outlook.Folder = _ TryCast(Application.Session.GetDefaultFolder( _ Outlook.OlDefaultFolders.olFolderInbox), Outlook.Folder) ' Retrieve a reference to the top-level ' folder. The GetFolder method expects to ' start at the top level, so you need ' this information. If inbox IsNot Nothing Then Dim parent As Outlook.Folder = _ TryCast(inbox.Parent, Outlook.Folder) If parent IsNot Nothing Then Dim folder As Outlook.Folder = _ GetFolder(parent.Name & "\Inbox\Customers\Archive") If folder IsNot Nothing Then folder.Display() End If End If End If End Sub
public void OnClick(Office.IRibbonControl control) { // Retrieve the name of the top-level folder, for // the purposes of this demonstration. Outlook.Folder inbox = Application.Session.GetDefaultFolder( Outlook.OlDefaultFolders.olFolderInbox) as Outlook.Folder; // Retrieve a reference to the top-level // folder. The GetFolder method expects to // start at the top level, so you need // this information. if (inbox != null) { Outlook.Folder parent = inbox.Parent as Outlook.Folder; if (parent != null) { Outlook.Folder folder = GetFolder(parent.Name + @"\Inbox\Customers\Archive"); if (folder != null) { folder.Display(); } } } }
Save your project, and press F5 to run it.
To edit an existing contact (or create one), on the Ribbon, click Add-Ins, and then click the new button.
If you built the add-in correctly, you can see the Inbox\Customers\Archive folder displayed in a new window.
Read It
The Microsoft Office Outlook 2007 object model provides several different ways to find a particular folder. If you are looking for the default folder of any particular type, such as Inbox or Contacts, you can use the Application.Session.GetFolder method, passing in one of the OlDefaultFolders enumerated values (as shown in the OnClick procedure in this article). If you are looking for a subfolder, you can retrieve a reference to the top-level folder, and use its Folders property to retrieve a specific subfolder (as shown in the GetFolder procedure in this article). If you know the EntryID or the StoreID value for a specific folder, you can call the Application.Session.GetFolderFromID method, although this method exists only to ease the transition between MAPI and Outlook, and is not as useful as the technique presented in this article for finding folders.
In this example, because the input to the GetFolder method is a string containing a full path, the code must attempt to locate each subfolder by name. Given a full path such as Mailbox—Your Name\Inbox\Customers\Archive, the example loops through the parsed folder names at each level by using the Folders property of an Outlook.Folder object to retrieve a reference to the subfolders. Because Outlook can index into its folders by name, at each level, the code attempts to retrieve a reference to the requested folder, given its name parsed from the full path. This solution relies on string parsing to find the path you need.