Click here to Skip to main content
13,352,280 members (71,170 online)
Click here to Skip to main content
Add your own
alternative version


43 bookmarked
Posted 29 Nov 2004

Adding CommandBars to Outlook 2003 using C#

, 29 Nov 2004
Rate this:
Please Sign up or sign in to vote.
How to add custom commandbars to Outlook 2003 using C#.


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", 

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", 

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"];

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 = 
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 = 
CommandBarControl myCommandBarControll = 
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 = 
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);


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


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


About the Author

Hannes Pavelka
Germany Germany
Feel free to visit my blog at

You may also be interested in...


Comments and Discussions

QuestionMessage Closed Pin
16-Feb-14 21:29
memberMember 1022658016-Feb-14 21:29 
GeneralMessage Closed Pin
7-Mar-10 7:09
memberMember 31108197-Mar-10 7:09 
Generalcustom items Pin
248912826-Jul-09 20:29
member248912826-Jul-09 20:29 
QuestionHow to add a new button to the menu bar in the child window(Appointment window, new message window etc) ? Pin
ramani1016-Jul-09 3:53
memberramani1016-Jul-09 3:53 
Generalbutton detection programatically Pin
SridharTadi28-May-09 4:08
memberSridharTadi28-May-09 4:08 
GeneralAdding a TextBox Pin
Phoen2516-May-08 7:22
memberPhoen2516-May-08 7:22 
QuestionHow To create sub menu in pop up window Pin
Swarna.060314-Mar-08 0:55
memberSwarna.060314-Mar-08 0:55 
QuestionHow to add a split on the menu? Pin
Ted Lin5-Jul-07 18:43
memberTed Lin5-Jul-07 18:43 
AnswerRe: How to add a split on the menu? Pin
Parijat Chandra27-Jul-07 5:27
memberParijat Chandra27-Jul-07 5:27 
GeneralRe: How to add a split on the menu? Pin
naveenj31-Jul-07 23:21
membernaveenj31-Jul-07 23:21 
QuestionHelp needed!! Pin
Anis Ur Rasul17-Apr-07 22:24
memberAnis Ur Rasul17-Apr-07 22:24 
GeneralOn Off Property Like find button Pin
butrflynlamb26-Feb-07 14:14
memberbutrflynlamb26-Feb-07 14:14 
GeneralThis info could save your click events life Pin
CHFIELDS12-May-06 4:11
memberCHFIELDS12-May-06 4:11 
GeneralCreating Additional tabs in Outlook Meeting/Appointment object Pin
Anonymous9-Aug-05 6:18
sussAnonymous9-Aug-05 6:18 
GeneralRe: Creating Additional tabs in Outlook Meeting/Appointment object Pin
haddati13-Aug-07 22:56
memberhaddati13-Aug-07 22:56 
QuestionHow to get the currently focused mail in outlook using c#? Pin
Inbam21-Jul-05 7:09
memberInbam21-Jul-05 7:09 
QuestionHow to get the currently focused mail in outlook using c#? Pin
Inbam21-Jul-05 7:09
memberInbam21-Jul-05 7:09 
AnswerRe: How to get the currently focused mail in outlook using c#? Pin
hoorayforlife13-Aug-06 14:02
memberhoorayforlife13-Aug-06 14:02 
GeneralProblem... Pin
laszlo varga14-Jul-05 23:08
susslaszlo varga14-Jul-05 23:08 
Generalthank u thank u thank u !!! Pin
JabraJabra12-Feb-05 3:59
memberJabraJabra12-Feb-05 3:59 
Generalcreating the "rudimentary" plug-in Pin
dorutzu29-Dec-04 0:00
memberdorutzu29-Dec-04 0:00 
GeneralRe: creating the "rudimentary" plug-in Pin
Hannes Pavelka30-Dec-04 10:51
memberHannes Pavelka30-Dec-04 10:51 
GeneralRe: creating the "rudimentary" plug-in Pin
dorutzu31-Dec-04 3:39
memberdorutzu31-Dec-04 3:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180111.1 | Last Updated 29 Nov 2004
Article Copyright 2004 by Hannes Pavelka
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid