Click here to Skip to main content
15,881,757 members
Articles / Programming Languages / C#
Article

Adding CommandBars to Outlook 2003 using C#

Rate me:
Please Sign up or sign in to vote.
4.52/5 (18 votes)
29 Nov 20043 min read 132.4K   774   43   23
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:

C#
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:

C#
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.

C#
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:

C#
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:

C#
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

C#
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.

C#
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.

C#
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:

C#
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.

C#
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


Written By
Germany Germany
Feel free to visit my blog at www.hannes-pavelka.com

Comments and Discussions

 
Generalcustom items Pin
248912826-Jul-09 19:29
248912826-Jul-09 19: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 2:53
ramani1016-Jul-09 2:53 
Generalbutton detection programatically Pin
SridharTadi28-May-09 3:08
SridharTadi28-May-09 3:08 
GeneralAdding a TextBox Pin
Phoen2516-May-08 6:22
Phoen2516-May-08 6:22 
QuestionHow To create sub menu in pop up window Pin
Swarna.060313-Mar-08 23:55
Swarna.060313-Mar-08 23:55 
QuestionHow to add a split on the menu? Pin
Ted Lin5-Jul-07 17:43
Ted Lin5-Jul-07 17:43 
AnswerRe: How to add a split on the menu? Pin
Parijat Chandra27-Jul-07 4:27
Parijat Chandra27-Jul-07 4:27 
GeneralRe: How to add a split on the menu? Pin
naveenj31-Jul-07 22:21
naveenj31-Jul-07 22:21 
QuestionHelp needed!! Pin
Anees Ur Rasool17-Apr-07 21:24
Anees Ur Rasool17-Apr-07 21:24 
GeneralOn Off Property Like find button Pin
butrflynlamb26-Feb-07 13:14
butrflynlamb26-Feb-07 13:14 
GeneralThis info could save your click events life Pin
CHFIELDS12-May-06 3:11
CHFIELDS12-May-06 3:11 
GeneralCreating Additional tabs in Outlook Meeting/Appointment object Pin
Anonymous9-Aug-05 5:18
Anonymous9-Aug-05 5:18 
GeneralRe: Creating Additional tabs in Outlook Meeting/Appointment object Pin
haddati13-Aug-07 21:56
haddati13-Aug-07 21:56 
QuestionHow to get the currently focused mail in outlook using c#? Pin
Inbam21-Jul-05 6:09
Inbam21-Jul-05 6:09 
QuestionHow to get the currently focused mail in outlook using c#? Pin
Inbam21-Jul-05 6:09
Inbam21-Jul-05 6:09 
AnswerRe: How to get the currently focused mail in outlook using c#? Pin
hoorayforlife13-Aug-06 13:02
hoorayforlife13-Aug-06 13:02 
GeneralProblem... Pin
laszlo varga14-Jul-05 22:08
laszlo varga14-Jul-05 22:08 
Generalthank u thank u thank u !!! Pin
JabraJabra12-Feb-05 2:59
JabraJabra12-Feb-05 2:59 
Generalcreating the "rudimentary" plug-in Pin
dorutzu28-Dec-04 23:00
dorutzu28-Dec-04 23:00 
GeneralRe: creating the "rudimentary" plug-in Pin
Hannes Pavelka30-Dec-04 9:51
Hannes Pavelka30-Dec-04 9:51 
GeneralRe: creating the "rudimentary" plug-in Pin
dorutzu31-Dec-04 2:39
dorutzu31-Dec-04 2: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.