O'Reilly logo

Writing Word Macros, Second Edition by Steven Roman PhD

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The CommandBar Object

As you have seen, the principal object related to menus and toolbars is the CommandBar object. Menubars, menus, submenus, shortcut menus, and toolbars are CommandBar objects. Note, however, that Office VBA does not treat each of these CommandBar objects in the same way. For instance, the Count property of the CommandBars collection counts only menubars, toolbars and shortcut menus. It does not count menus or submenus. Also, as mentioned earlier, the Add method of the CommandBars collection allows can be used to create toolbars or menubars, but not menus or submenus.

The CommandBar object has a Type property that can assume one of the constants in the following enum:

	Enum MsoBarType
	   msoBarTypeNormal = 0    ' toolbar
	   msoBarTypeMenuBar = 1   ' menubar
	   msoBarTypePopup = 2     ' menu, submenu or shortcut menu
	End Enum

Creating a New Menubar or Toolbar

As we have said, one way in which menubars and toolbars differ from menus and submenus is in their creation. To create a new menubar or shortcut menu, we use the Add method of the CommandBars collection. The syntax for the Add method is:

CommandBarsObject.Add(Name, Position, MenuBar, Temporary)

The optional Name parameter is the name of the new command bar. If this argument is omitted, Word VBA assigns a default name (such as “Custom 1”) to the command bar. The optional Position parameter gives the position of the new command bar. This can be set to msoBarLeft, msoBarTop, msoBarRight, msoBarBottom,or msoBarFloating (for a floating ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required