Microsoft Outlook 2010 introduces a new feature called the Solutions Module that you can use to add a custom navigation module to the Outlook Navigation Pane programmatically. Once you add your module, you can add your own custom Outlook folders to it and provide custom icons for each to distinguish them from the built-in folders in Outlook.
This Visual How To shows how to create a Visual Studio Tools for Office add-in that creates a custom solution. In the process, you customize the Microsoft Office Fluent Ribbon with a button that invokes your code.
You can use a Visual Studio Tools for Office add-in to run your compiled code inside Outlook and to interact with the Outlook object model.
To create the add-in
In Visual Studio 2010, create an add-in for Outlook 2010. Name the new project CustomSolutionModule.
In the Solution Explorer window, right-click the project, select Add from the context menu, and then click New Item.
In the Add New Item dialog box, under Common Items, select Office, and then select Ribbon (Visual Designer).
Change the Name to SolutionModuleRibbon and then click Add.
Use the following procedure to add a button to the ribbon that invokes your code.
Select the ribbon and change its RibbonType property to Microsoft.Outlook.Explorer.
From the toolbox, add a RibbonButton.
Change the button's Name property to btnCreateSM.
Change the button's Label property to Show It.
Change the existing Label property of the RibbonGroup to Custom App.
Use the following procedure to add the code that your add-in executes when the user clicks the RibbonButton.
Add a Click event handler for the button.
In the Click event handler for the RibbonButton, add the following Try Catch block.
Try
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, ex.Source)
End Try
Inside the Try block, add the following code to define the objects that you will need.
Const DISPLAY_POS As Integer = 5
Dim app As Outlook.Application = Globals.ThisAddIn.Application
Dim exp As Outlook.Explorer =
app.ActiveExplorer()
Dim np As Outlook.NavigationPane =
exp.NavigationPane
Dim sm As Outlook.SolutionsModule =
TryCast(np.Modules.GetNavigationModule(
Outlook.OlNavigationModuleType.olModuleSolutions),
Outlook.SolutionsModule)
Dim solutionRoot As Outlook.Folder = Nothing
Dim solutionCalendar As Outlook.Folder = Nothing
Dim solutionTasks As Outlook.Folder = Nothing
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, ex.Source)
Next, add the following code to get the root folder of the default Outlook store.
Dim rootStoreFolder As Outlook.Folder =
app.Session.DefaultStore.GetRootFolder()
With the next bit of code, you run a LINQ query to see whether the root folder of your solution is already defined.
Dim results = From aFolder As Outlook.Folder
In rootStoreFolder.Folders
Where aFolder.Name = "Custom App"
Select aFolder
Once the query runs, the following code checks whether you got back your root folder. If you have, you access your existing folders. If you have not, you must create them and add your custom solution to the Solutions Module.
If results.Count > 0 Then
solutionRoot = results(0)
solutionCalendar = TryCast(
solutionRoot.Folders("Custom Calendar"), Outlook.Folder)
solutionTasks = TryCast(
solutionRoot.Folders("Custom Tasks"), Outlook.Folder)
Else
solutionRoot = TryCast(rootStoreFolder.Folders.Add(
"Custom App", Outlook.OlDefaultFolders.olFolderInbox),
Outlook.Folder)
solutionCalendar = TryCast(solutionRoot.Folders.Add(
"Custom Calendar", Outlook.OlDefaultFolders.olFolderCalendar),
Outlook.Folder)
solutionTasks = TryCast(solutionRoot.Folders.Add(
"Custom Tasks", Outlook.OlDefaultFolders.olFolderTasks),
Outlook.Folder)
End If
sm.AddSolution(solutionRoot,
Outlook.OlSolutionScope.olHideInDefaultModules)
Finally, add the following code to make your solution visible in the Navigation Pane.
If Not sm.Visible Then
sm.Visible = True
End If
If Not sm.Position = DISPLAY_POS Then
sm.Position = DISPLAY_POSEnd If
If Not np.DisplayedModuleCount = DISPLAY_POS Then
np.DisplayedModuleCount = DISPLAY_POS
End If
Save your work.
Press F5 to start your add-in in debug mode.
Click the Add-ins tab.
Click Show It to see your new solution.
Note
The code that the add-in runs during the button click is typically put in the Startup method of your add-in. It is only done here for ease of debugging.
Close Outlook and return to Visual Studio.
When you create a custom solutions module, you can add your own icons for your custom folders.
Note
If you do not have your own custom icons, you can use one of the icons included with Visual Studio 2010 in the zip file VS2010ImageLibrary.zip that is located in %Program Files x86%\Microsoft Visual Studio 10.0\Common7\VS2010ImageLibrary\%localid% by default.
To add custom icons to your folders
|
Watch the video
> [!VIDEO https://www.microsoft.com/en-us/videoplayer/embed/d5599b10-9bcd-4b70-a654-1dd9ebbfe1eb]
Length: 00:7:47
Grab the Code
|