Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Adding CommandBars to Outlook 2003 using C#

0.00/5 (No votes)
29 Nov 2004 2  
How to add custom commandbars to Outlook 2003 using C#.

Introduction

I assume you have a rudimentary plug-in up and running because I won't show you how to actually create a plug-in in this article. There are plenty of good articles out there covering this topic. Since all Office applications share the same commandbars, this article applies to other Office products as well.

Overview of the CommandBar Objects

Let us take a brief look at how the structure of the Commandbar Objects looks like:

  • CommandBars (CommandBar)
  • CommandBarControls (CommandBarControl)
  • CommandBarButton
  • CommandBarCombBox
  • CommandBarPopup

The CommandBars Collection

The CommandBars collection object represent the command bars in the container application, thus naturally it contains CommandBar objects.

Adding a new CommandBar is straightforward:

applicationObject.ActiveExplorer().CommandBars.Add(object name, 
          object position, object menubar, object temporary);

What gives us a little trouble is the fact that all parameters are accepting objects. Let's examine the parameters in more detail and what type it expects:

  • object name -> string name

    The name of the new Command Bar.

  • object position -> MsoBarPosition position

    The position of the command bar. Expects a value from the MsoBarPosition enumeration. msoBarLeft, msoBarTop, msoBarRight, msoBarBottom indicate the left, top, right, and bottom coordinates of the new command bar. msoBarFloating indicates that the new command bar won't be docked. msoBarPopup indicates that the new command bar will be a shortcut menu. msoBarMenuBar applies to Macintosh only.

  • object menubar -> bool menubar

    true to replace the active menu bar with the new command bar. The default value is false.

  • object temporary -> bool temporary

    true to make the command bar temporary. Temporary command bars are deleted when the container application is closed. The default value is false.

Add a new Command Bar to the CommandBars Collections

It's pretty easy to add a new bar:

applicationObject.ActiveExplorer().CommandBars.Add ("custom", 
          MsoBarPosition.msoBarTop,false,true);

The Command Bar object

Adding a CommandBar to the CommandBars collections returns us a reference to the newly created command bar.

CommandBar myCommandBar = 
  applicationObject.ActiveExplorer().CommandBars.Add ("custom", 
  MsoBarPosition.msoBarTop,false,true);

If you executed the above code, you might have noticed your new command bar is not visible. You can modify this by simply setting myCommandBar.visible = true;.

The CommandBar object is important if you want to add a new control to an existing command bar. Adding a command bar is easy:

CommandBar myCommandBar = 
  applicationObject.ActiveExplorer().CommandBars["Menu Bar"];

This line of code gets us the Main Menu from Outlook. Adding a new item to the Main Menu is straightforward:

myCommandBar.Controls.Add(object type, object id, 
       object parameter, object before, object temporary);

Again, we need to examine the parameters a bit closer:

  • object type -> MsoControlType type

    The type of the control. Expects a value from the MsoControlType Enumeration.

  • object id -> int ID

    An integer that specifies a built-in control. If the value is 1, or if omitted, a blank custom control of the specified type will be added to the command bar. You can omit a value by using the Missing.Value property from the System.Reflection namespace, otherwise simply use 1;

  • object parameter -> object parameter

    This parameter works like the "TAG" property of normal Windows controls. You can store any user defined data here or simply omit the parameter.

  • object before -> int before

    A number that indicates the position of the new control on the command bar. The new control will be inserted before the control at this position. If omitted, the control is added at the end of the command bar.

  • object temporary -> bool temporary

    true to make the command bar temporary. Temporary command bars are deleted when the container application is closed. The default value is false.

Modify an existing command bar

CommandBar myCommandBar = 
   applicationObject.ActiveExplorer).CommandBars["Menu Bar"];
CommandBar.Controls.Add(MsoControlType.msoControlPopup, 
   1,"",1,true);

Adding a new control to an existing command bar returns us a reference to our new CommandBar control. Using this reference, you can make the newly created menu item visible and set its caption.

CommandBarControl myCommandBarControl = 
  myCommandBar.Controls.Add(MsoControlType.msoControlPopup,1,"",1,true);
myCommandBarControl.Visible = true;
myCommandBarControl.Caption ="DEMO";

Modify the existing tool menu item

Let's now try to modify the existing tool menu and add a msoControlPopup. A msoControlPopup is a submenu item which can be expanded and shows you more options to pick from.

CommandBar myCommandBar = 
      applicationObject.ActiveExplorer().CommandBars["Tools"];
CommandBarControl myCommandBarControll = 
      myCommandBar.Add(MsoControlType.msoControlPopup,1,"",1,true);
myCommandBar.Visible = true;
cmdBarPopup.Caption ="Popup";

Now, we need to add a sub button to the popup button. We can do this with the following code:

CommandBarPopup popup = (CommandBarPopup) myCommandBarControll;
CommandBar bar = popup.CommandBar;
CommandBarControl cmdBarControl = 
   bar.Controls.Add(MsoControlType.msoControlButton,1,"",1,true);
cmdBarControl.Caption ="subbutton";

Adding Events

Once we have our Command Bars arranged to our needs, we can start adding our event handlers.

CommandBarButton button = (CommandBarButton)cmdBarControl;
button.Click += new _CommandBarButtonsEvents_ClickEventHandler(button_click);

Conclusion

Hopefully, this article was helpful for you. I am looking forward to your feedback.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here