Developing a VSTO Addin for Outlook – Getting started
A module or a solution module is a navigation module in Microsoft Outlook. It can let you expose your folder in a separate navigation pane just like other modules like Calendar, Tasks etc in Outlook. Then you can customize the folder view pane and the reading pane for the folder items. In this article, we will develop our own solution module.


Creating a folder
Let us start by creating a simple VSTO addin as explained in the Getting started article. In Outlook, there is a concept of folders. Every module is a folder, be it MailModule, CalendarModule, TasksModule etc. Therefore, we also need to create our own say “MyCustomAddin” folder in the root folder of Outlook. Let us first check how to create a folder programmatically given any parent folder with or without a specified folder type.
/// <summary> /// Create a folder if it doesn't exist /// </summary> /// <param name="parent">Parent folder</param> /// <param name="folderName">Folder name</param> /// <returns>Create/already existing folder</returns> private Outlook.Folder GetOrCreateFolder(Outlook.Folder parent, string folderName) { Outlook.Folder folder = parent.Folders.Cast<Outlook.Folder>() .FirstOrDefault(f => f.Name == folderName); //if the folder doesn't exist, then create it if (folder == null) { folder = parent.Folders.Add(folderName) as Outlook.Folder; } return folder; } /// <summary> /// Create a folder if it doesn't exist /// </summary> /// <param name="parent">Parent folder</param> /// <param name="folderName">Folder name</param> /// <param name="folderType">Folder type</param> /// <returns>Create/already existing folder</returns> private Outlook.Folder GetOrCreateFolder(Outlook.Folder parent, string folderName, Outlook.OlDefaultFolders folderType) { Outlook.Folder folder = parent.Folders.Cast<Outlook.Folder>().FirstOrDefault(f => f.Name == folderName); //if the folder doesn't exist, then create it if (folder == null) { folder = parent.Folders.Add(folderName, folderType) as Outlook.Folder; } return folder; }
Now get the root folder and add our module folder. The folder inside the root folder in outlook has to be one of the default folder type. In our case, we are using the Inbox folder type. Outlook will then assign the default characteristics like view of the Inbox to our new module folder. You can however easily modify that later on.
//Instance of the current outlook application Outlook.Application app = Globals.ThisAddIn.Application; //Get the root folder Outlook.Folder rootStoreFolder = app.Session.DefaultStore.GetRootFolder() as Outlook.Folder; //Add our module folder Outlook.Folder moduleRoot = GetOrCreateFolder(rootStoreFolder, "MyCustomAddin", Outlook.OlDefaultFolders.olFolderInbox);
Adding a solution module
Once our module folder is created, we will add our folder to the solution module collection of outlook and this will create the new solution module in outlook navigation pane.
//Get the active explorer Outlook.Explorer explorer = app.ActiveExplorer(); //Get the navigation pane from the explorer Outlook.NavigationPane navPane = explorer.NavigationPane; //Get the solution modules from the navigation pane Outlook.SolutionsModule solutionsModule = navPane.Modules .GetNavigationModule(Outlook.OlNavigationModuleType.olModuleSolutions) as Outlook.SolutionsModule; //Add our folder to the modules collection solutionsModule.AddSolution(moduleRoot, Outlook.OlSolutionScope.olHideInDefaultModules); if (solutionsModule.Visible == false) solutionsModule.Visible = true;
We can also attach our own event handler to explorer’s folder switch or navigation pane’s module switch in order to do any custom setting that we might need to do when our module folder is activated.
Complete implementation
Thus after a little clean up, our complete class would look like this.
using System.Linq; using Outlook = Microsoft.Office.Interop.Outlook; namespace MyOutlookAddin { public class Core { private static Core _instance; public static Core Instance { get { if (_instance == null) _instance = new Core(); return _instance; } } Outlook.Application app; Outlook.Explorer explorer; Outlook.NavigationPane navPane; Outlook.SolutionsModule solutionsModule; private Core() { } public void Initialize() { //Instance of the current outlook application app = Globals.ThisAddIn.Application; //Get the root folder Outlook.Folder rootStoreFolder = app.Session.DefaultStore.GetRootFolder() as Outlook.Folder; //Add our module folder Outlook.Folder moduleRoot = GetOrCreateFolder(rootStoreFolder, "MyCustomAddin", Outlook.OlDefaultFolders.olFolderInbox); //Get the active explorer explorer = app.ActiveExplorer(); //Get the navigation pane from the explorer navPane = explorer.NavigationPane; //Get the solution modules from the navigation pane solutionsModule = navPane.Modules .GetNavigationModule(Outlook.OlNavigationModuleType.olModuleSolutions) as Outlook.SolutionsModule; //Add our folder to the modules collection solutionsModule.AddSolution(moduleRoot, Outlook.OlSolutionScope.olHideInDefaultModules); if (solutionsModule.Visible == false) solutionsModule.Visible = true; explorer.FolderSwitch += Explorer_FolderSwitch; navPane.ModuleSwitch += NavPane_ModuleSwitch; } private void NavPane_ModuleSwitch(Outlook.NavigationModule CurrentModule) { //Add your own implementation } private void Explorer_FolderSwitch() { //Add your own implementation } /// <summary> /// Create a folder if it doesn't exist /// </summary> /// <param name="parent">Parent folder</param> /// <param name="folderName">Folder name</param> /// <returns>Create/already existing folder</returns> private Outlook.Folder GetOrCreateFolder(Outlook.Folder parent, string folderName) { Outlook.Folder folder = parent.Folders.Cast<Outlook.Folder>() .FirstOrDefault(f => f.Name == folderName); //if the folder doesn't exist, then create it if (folder == null) { folder = parent.Folders.Add(folderName) as Outlook.Folder; } return folder; } /// <summary> /// Create a folder if it doesn't exist /// </summary> /// <param name="parent">Parent folder</param> /// <param name="folderName">Folder name</param> /// <param name="folderType">Folder type</param> /// <returns>Create/already existing folder</returns> private Outlook.Folder GetOrCreateFolder(Outlook.Folder parent, string folderName, Outlook.OlDefaultFolders folderType) { Outlook.Folder folder = parent.Folders.Cast<Outlook.Folder>().FirstOrDefault(f => f.Name == folderName); //if the folder doesn't exist, then create it if (folder == null) { folder = parent.Folders.Add(folderName, folderType) as Outlook.Folder; } return folder; } } }
Perfect! Now just run the code and check out your new solution module. Click the ellipsis and change the navigation order of the Add-ins. Click OK and click the Add-ins icon to show your own module and folder.


Please comment below for any questions. Thanks a lot.
0 Comments