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.
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
In Visual Studio 2010, create an add-in for Outlook 2010. Name the new project OutlookExchangeInfo.
In the Solution Explorer window, right-click the project, select Add from the context menu, and then click Windows Form.
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.
Add a Panel control to the Form and dock it to the bottom of the Form.
Add a Button inside the Panel on the Form. Change the button's Text to Close and its DialogResult property to OK.
Next, add a SplitContainer control to the form. Set its Dock Property to Fill.
Add a TreeView control to the left panel of the SplitContainer. Name it treeAccounts. Dock the control inside the pane.
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.
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
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
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
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
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 MainRibbon and then click Add.
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 btnGetInfo.
Change the button's Label property to Get Information.
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.
Add a Click event handler for the button.
Import the System.Windows.Forms namespace in the code-behind file for the ribbon.
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
Save your work.
Press F5 to start your add-in in debug mode.
Click the Add-ins tab.
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
Open MainRibbon.vb in the ribbon designer in Visual Studio.
From the toolbox, add a RibbonButton.
Change the button's Name property to btnCreateMail.
Change the button's Label property to Create Mail.
Now add a click event handler that creates a new e-mail message.
Add a Click event handler for the button.
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
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
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
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
Save your work.
Press F5 to start your add-in in debug mode.
Click the Add-ins tab.
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.
|
Watch the video
> [!VIDEO https://www.microsoft.com/en-us/videoplayer/embed/565e4317-57e1-4130-80a4-1f866ef692bf]
Length: 00:8:01
Grab the Code
|