Adding a Menu Item

It is worth mentioning now that there are a few counterintuitive wrinkles in the process of menu creation. In particular, we might think at first that adding a new menu should be done using the Add method of the CommandBars collection, specifying the name of the parent menu and the location of the new menu on the parent. After all, a menu is a CommandBar object, and this procedure would be consistent with other cases of adding objects to a collection.

However, this is not how it is done. Instead, as we will see, a new menu (or submenu) is created by adding a command-bar control of type msoControlPopup to the CommandBarControls collection of the parent menu (and specifying the new control's position on the parent). Actually, this represents a savings of effort on our behalf. For, as we have remarked, a menu or submenu requires a popup control for activation. Thus, Microsoft makes the task of creating menus and submenus easier by automatically creating the corresponding (empty) menu or submenu in response to our creation of a popup control. (We will see an example of this later, so don't worry too much if this is not perfectly clear yet.)

One word of advice before proceeding: As we will see, when creating a new toolbar or menu, you can set one of the parameters to make the object temporary, meaning that it will be destroyed when Excel is closed. In this way, if anything unexpected happens, it is easy to recover—just close Excel and reopen it. Alternatively, by opening the Customize dialog box (from the Tools menu), you can delete menu items by dragging them off of the menu, and you can delete toolbars by using the Delete button.

0 0

Post a comment