Click here to Skip to main content
Click here to Skip to main content
Go to top

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

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

Share

About the Author

Hannes Pavelka

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

Comments and Discussions

 
QuestionMessage Automatically Removed PinmemberMember 1022658016-Feb-14 20:29 
GeneralMessage Automatically Removed PinmemberMember 31108197-Mar-10 6:09 
Generalcustom items Pinmember248912826-Jul-09 19:29 
QuestionHow to add a new button to the menu bar in the child window(Appointment window, new message window etc) ? Pinmemberramani1016-Jul-09 2:53 
Generalbutton detection programatically PinmemberSridharTadi28-May-09 3:08 
GeneralAdding a TextBox PinmemberPhoen2516-May-08 6:22 
QuestionHow To create sub menu in pop up window PinmemberSwarna.060313-Mar-08 23:55 
QuestionHow to add a split on the menu? PinmemberTed Lin5-Jul-07 17:43 
AnswerRe: How to add a split on the menu? PinmemberParijat Chandra27-Jul-07 4:27 
GeneralRe: How to add a split on the menu? Pinmembernaveenj31-Jul-07 22:21 
QuestionHelp needed!! PinmemberAnis Ur Rasul17-Apr-07 21:24 
GeneralOn Off Property Like find button Pinmemberbutrflynlamb26-Feb-07 13:14 
GeneralThis info could save your click events life PinmemberCHFIELDS12-May-06 3:11 
GeneralCreating Additional tabs in Outlook Meeting/Appointment object PinsussAnonymous9-Aug-05 5:18 
GeneralRe: Creating Additional tabs in Outlook Meeting/Appointment object Pinmemberhaddati13-Aug-07 21:56 
QuestionHow to get the currently focused mail in outlook using c#? PinmemberInbam21-Jul-05 6:09 
QuestionHow to get the currently focused mail in outlook using c#? PinmemberInbam21-Jul-05 6:09 
AnswerRe: How to get the currently focused mail in outlook using c#? Pinmemberhoorayforlife13-Aug-06 13:02 
GeneralProblem... Pinsusslaszlo varga14-Jul-05 22:08 
Generalthank u thank u thank u !!! PinmemberJabraJabra12-Feb-05 2:59 
Generalcreating the "rudimentary" plug-in Pinmemberdorutzu28-Dec-04 23:00 
GeneralRe: creating the "rudimentary" plug-in PinmemberHannes Pavelka30-Dec-04 9:51 
GeneralRe: creating the "rudimentary" plug-in Pinmemberdorutzu31-Dec-04 2:39 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 29 Nov 2004
Article Copyright 2004 by Hannes Pavelka
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid