Our first order of business is to build a user interface for our Add-in's functionality. Given that an Add-in's UI is plugged into the hosting application, we will define a new member variable of type Microsoft.Office.Core.CommandBarButton to the Connect class type:
public class Connect : Object, Extensibility.IDTExtensibility2 { ... // Our UI will consist of a single CommandBarButton private CommandBarButton btnGetEMailStats; }
The CommandBarButton widget (which sports the caption 'Statistics') will be plugged into Outlook's Standard command bar (Figure 10):
Figure 10. Our custom Statistics CommandBarButton
As mentioned, the OnStartupComplete() method is an ideal place to build UI elements, given that the host has fully come to life. Here are the steps required to insert new CommandBarButton types into an existing command bar:
That being said, here is the updated (and highly annotated) implementation of OnStartupComplete():
public void OnStartupComplete(ref System.Array custom) { // First, get access to the CommandBars on // the active explorer. CommandBars commandBars = applicationObject.ActiveExplorer().CommandBars; try { // If our button is already // on the Standard CommandBar, use it. btnGetEMailStats = (CommandBarButton) commandBars["Standard"].Controls["Statistics"]; } catch { // OOPS! Our button is not there, so // we need to make a new instance. // Note that the Add() method was // defined to take optional parameters, // which are not supported in C#. // Thus we must specify Missing.Value. btnGetEMailStats = (CommandBarButton) commandBars["Standard"].Controls.Add(1, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value); btnGetEMailStats.Caption = "Statistics"; btnGetEMailStats.Style = MsoButtonStyle.msoButtonCaption; } // Setting the Tag property is not required, but can be used // to quickly reterive your button. btnGetEMailStats.Tag = "Statistics"; // Setting OnAction is also optional, however if you specify // the ProgID of the Add-in, the host will automatically // load the Add-in if the user clicks on the CommandBarButton when // the Add-in is not loaded. After this point, the Click // event handler is called. btnGetEMailStats.OnAction = "!<EMailStatsAddIn.Connect>"; btnGetEMailStats.Visible = true; // Rig-up the Click event for the new CommandBarButton type. btnGetEMailStats.Click += new _CommandBarButtonEvents_ClickEventHandler( btnGetEMailStats_Click); }
Notice that our CommandBarButton has been configured to call a method named btnGetEMailStats_Click() when clicked. We will implement the custom logic in just a moment, but the following stub code will do for now:
private void btnGetEMailStats_Click(CommandBarButton Ctrl, ref bool CancelDefault) { // ToDo: Implement custom logic. }