共用方式為


Manipulating Multiple Exchange Accounts in Outlook 2010

Office Visual How To

Summary:  Create an add-in to manipulate multiple Exchange accounts that are configured in a single Microsoft Outlook 2010 profile.

Applies to: Office 2010 | Outlook 2010 | Visual Studio

Published:   December 2009

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

Overview

In Microsoft Outlook 2010, you can assign more than one Exchange account to a profile; updates to the Outlook 2010 object model support the functionality. This Visual How To explains how to create an add-in by using Visual Studio Tools for Office 2010 to access Exchange account information.

Code It

Your add-in can use the new Exchange account information to interact more effectively with a user's Outlook 2010 profile.

This Visual How To shows how to add a button to the Microsoft Office Fluent user interface Ribbon on the active Outlook Explorer window. Users click the button to open a Windows Forms dialog box that displays information about all of the Exchange accounts that are configured for the current profile.

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

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

  3. Change the Name to DisplayAccountInfo and then click Add.

Use the following procedure to add several controls that display data about the configured accounts in the user's Outlook session.

To add controls to the form

  1. Add a Panel control to the Form and dock it to the bottom of the Form.

  2. Add a Button inside the Panel on the Form. Change the button's Text to Close and its DialogResult property to OK.

  3. Next, add a SplitContainer control to the form. Set its Dock Property to Fill.

  4. Add a TreeView control to the left panel of the SplitContainer. Name it treeAccounts. Dock the control inside the pane.

  5. Add a TreeView control to the right panel of the SplitContainer. Name it listProperties. Dock the control inside the pane.

With the controls in place, it is time to write some code that can put the information into the controls.

To add methods to the form's code-behind file

Use the following procedure to add a custom tab to the ribbon of the Explorer window.

To customize the Office Fluent user interface by adding a custom tab to the ribbon

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

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

  3. Change the Name to MainRibbon and then click Add.

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

  5. From the toolbox, add a RibbonButton.

  6. Change the button's Name property to btnGetInfo.

  7. Change the button's Label property to Get Information.

  8. Change the existing Label property of the RibbonGroup to Exchange Info.

Use the following procedure to add a click event handler so that when the user clicks the button on the ribbon, you can load the form that you created earlier.

To add a click event handler for the button

With multiple Exchange accounts in a profile, you can now dynamically retrieve the correct user information, based on where the user is working in Outlook.

Use the following procedure to add a second button to the ribbon that creates a new message and correctly sets the From field based on the active Exchange account.

To create a context-aware e-mail message

  1. Open MainRibbon.vb in the ribbon designer in Visual Studio.

  2. From the toolbox, add a RibbonButton.

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

  4. Change the button's Label property to Create Mail.

Now add a click event handler that creates a new e-mail message.

To add a click event handler for the second button

  1. Add a Click event handler for the button.

  2. In the Click event handler for the RibbonButton, add a Try…Catch block of code.

    Try
    
    Catch ex As Exception
      Dim errorText As String =
          String.Format(
              "An unexpected error occurred.{0}Error details:{0}{1}",
              Environment.NewLine, ex.Message)
      MessageBox.Show(
          errorText,
          ex.Source,
          MessageBoxButtons.OK, MessageBoxIcon.Error)
    
    End Try
  3. Inside the Try block, define several variables and determine the active user store based on the user's selection.

    Dim host As Outlook.Application = Globals.ThisAddIn.Application
    Dim fld As Outlook.Folder =
      TryCast(host.ActiveExplorer.CurrentFolder, Outlook.Folder)
    Dim str As Outlook.Store = fld.Store
  4. Next, add the following LINQ query to locate the user account that matches the current store.

    Dim results = From exAccts As Outlook.Account
      In host.Session.Accounts
      Where exAccts.DeliveryStore.StoreID = str.StoreID
      Select exAccts
  5. Finally, if the query finds the user's account object, the following code creates an e-mail message and sets the From property to the correct account by using the AddressEntry object.

    If results.Count > 0 Then
      Dim ae As Outlook.AddressEntry =
        results(0).CurrentUser.AddressEntry
      Dim mail As Outlook.MailItem =
      host.CreateItem(Outlook.OlItemType.olMailItem)
    
      If ae IsNot Nothing Then
        mail.Sender = ae
      End If
      mail.Subject = "E-mail created by add-in"
      mail.BodyFormat = Outlook.OlBodyFormat.olFormatPlain
      mail.Body = "Having multiple Exchange accounts is great!"
      mail.Display(False)
    Else
      MessageBox.Show(
      "Could not match account to active session.",
      "Information",
      MessageBoxButtons.OK, MessageBoxIcon.Information)
    End If
  6. Save your work.

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

  8. Click the Add-ins tab.

  9. Click the Create Mail button to try it out. If everything works, you will see the From field set to the correct address, based on the selected folder.

See It

Watch the video

> [!VIDEO https://www.microsoft.com/en-us/videoplayer/embed/565e4317-57e1-4130-80a4-1f866ef692bf]

Length: 00:8:01

Click to grab code

Grab the Code

Explore It