共用方式為


Adding Solution-Specific Folders to the Solutions Module in Outlook 2010

Office Visual How To

Summary:  Create a Visual Studio Tools for Office add-in to add custom solution-specific folders to the Microsoft Outlook 2010 Solutions Module.

Applies to: Office 2010 | Outlook 2010 | Visual Studio

Published:  December 2009

Provided by:  Brian A. Randell, MCW Technologies, LLC

Overview

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.

Code It

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

  1. In Visual Studio 2010, create an add-in for Outlook 2010. Name the new project CustomSolutionModule.

  2. In the Solution Explorer window, right-click the project, select Add from the context menu, and then click New Item.

  3. In the Add New Item dialog box, under Common Items, select Office, and then select Ribbon (Visual Designer).

  4. Change the Name to SolutionModuleRibbon and then click Add.

Use the following procedure to add a button to the ribbon that invokes your code.

To add the button to the ribbon

  1. Select the ribbon and change its RibbonType property to Microsoft.Outlook.Explorer.

  2. From the toolbox, add a RibbonButton.

  3. Change the button's Name property to btnCreateSM.

  4. Change the button's Label property to Show It.

  5. 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.

To add the code to the button

  1. Add a Click event handler for the button.

  2. 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
  3. 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)
  4. Next, add the following code to get the root folder of the default Outlook store.

    Dim rootStoreFolder As Outlook.Folder =
      app.Session.DefaultStore.GetRootFolder()
  5. 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
  6. 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)
  7. 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
  8. Save your work.

  9. Press F5 to start your add-in in debug mode.

  10. Click the Add-ins tab.

  11. 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.

  12. 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

  1. Add PictureDispConverter.vb to your Visual Studio Project. This source file, which you can find in the sample code, provides helper functions that convert icons to the correct format for Outlook.

  2. Add three icons to your project by putting them in a Resources folder.

  3. Change the Build Action for each icon to Embedded Resource by using the Visual Studio Properties window.

  4. Add the following code, making sure that you replace the string "icon.ico" with the file name of the icons that you added earlier. This code should appear before the code If Not sm.Visible.

    Dim rootIcon As Icon =
      New Icon([GetType](), "icon.ico")
    Dim calIcon As Icon =
      New Icon([GetType](), "icon.ico")
    Dim taskIcon As Icon =
      New Icon([GetType](), "icon.ico")
    
    solutionRoot.SetCustomIcon(
        PictureDispConverter.ToIPictureDisp(rootIcon))
    solutionCalendar.SetCustomIcon(
      PictureDispConverter.ToIPictureDisp(calIcon))
    solutionTasks.SetCustomIcon(
      PictureDispConverter.ToIPictureDisp(taskIcon))
    If Not sm.Visible Then
  5. Save your work.

  6. Press F5 to start your add-in in debug mode.

  7. Click the Add-ins tab.

  8. Click Show It to see your new solution. When you access your solutions module, you should see your three custom icons.

See It

Watch the video

> [!VIDEO https://www.microsoft.com/en-us/videoplayer/embed/d5599b10-9bcd-4b70-a654-1dd9ebbfe1eb]

Length: 00:7:47

Click to grab code

Grab the Code

Explore It