Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Manipulating Multiple Exchange Accounts in Outlook 2010

Office 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:    Microsoft Outlook 2010

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

  1. Add the AddAccount method.

    Public Sub AddAccount(
      ByVal acct As Outlook.Account)
        Dim node As TreeNode
        node = treeAccounts.Nodes.Add(
      acct.DisplayName)
        node.Tag = acct
    End Sub
    
    
  2. Next, add the AddNewItem method.

    Private Sub AddNewItem(ByVal PropName As String,
                           ByVal PropValue As String)
        Dim lvItem As ListViewItem =
            listProperties.Items.Add(PropName)
        lvItem.SubItems.Add(PropValue)
    End Sub
    
    
  3. Finally, add the following code to the treeAccount control's AfterSelect event.

    listProperties.Items.Clear()
    Dim exAcct As Outlook.Account =
        TryCast(e.Node.Tag, Outlook.Account)
    If exAcct Is Nothing Then
        listProperties.Items.Add("Account Object Unavailable.")
    Else
        AddNewItem("AccountName",
          System.Enum.GetName(GetType(Outlook.OlAccountType),
      exAcct.AccountType))
        AddNewItem("AutoDiscoverConnectionMode",
          System.Enum.GetName(GetType(Outlook.OlAutoDiscoverConnectionMode),
      exAcct.AutoDiscoverConnectionMode))
        AddNewItem("AutoDiscoverXml", exAcct.AutoDiscoverXml)
        AddNewItem("CurrentUser.Name", exAcct.CurrentUser.Name)
        AddNewItem("DeliveryStore.DisplayName",
           exAcct.DeliveryStore.DisplayName)
        AddNewItem("ExchangeConnectionMode",
          System.Enum.GetName(GetType(Outlook.OlExchangeConnectionMode),
      exAcct.ExchangeConnectionMode))
        AddNewItem("ExchangeMailboxServerName",
           exAcct.ExchangeMailboxServerName)
        AddNewItem("ExchangeMailboxServerVersion",
           exAcct.ExchangeMailboxServerVersion)
    End If
    
  4. Save your work.

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

  1. Add a Click event handler for the button.

  2. Import the System.Windows.Forms namespace in the code-behind file for the ribbon.

  3. In the Click event handler for the RibbonButton, add the following code.

    Try
        Dim host As Outlook.Application = Globals.ThisAddIn.Application
        ' Check for Outlook 2010
        If host.Version.Substring(0, 2) = "14" Then
            Dim results = From exAccts As Outlook.Account In host.Session.Accounts
                      Where exAccts.AccountType = Outlook.OlAccountType.olExchange
                      Select exAccts
    
            If results.Count > 0 Then
                Using frm As New DisplayAccountInfo
                    For Each exAcct As Outlook.Account In results
                        frm.AddAccount(exAcct)
                    Next
                    frm.ShowDialog()
                End Using
            Else
                MessageBox.Show(
                    "No Exchange Accounts found in current profile.",
                    "Information",
                    MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
        End If
    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
    
  4. Save your work.

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

  6. Click the Add-ins tab.

  7. Click Get Information to try it out.

    When you are satisfied, exit Outlook 2010 and return to Visual Studio.

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

Watch video

Length: 00:8:01

Click to grab code

Grab the Code

Explore It
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.