Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 99 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
99
Dung lượng
0,95 MB
Nội dung
654 Part VI ✦ Developing Applications Toolbar Manipulations The following list summarizes the ways in which you can customize toolbars in Excel: ✦ Remove controls from built-in toolbars. You can get rid of controls that you never use and free up a few pixels of screen space. ✦ Add controls to built-in toolbars. You can add as many controls as you want to any toolbar. These controls can be custom buttons or buttons from other tool- bars, or they can come from the stock of controls that Excel provides. ✦ Create new toolbars. You can create as many new toolbars as you like, with toolbar controls from any source. ✦ Change the functionality of built-in toolbar controls. You do this by attaching your own macro to a built-in control. ✦ Change the image that appears on any toolbar control. Excel includes a rudi- mentary but functional toolbar button editor, although there are several other image-changing techniques. You can perform these customizations manually by using the Customize dialog box. This dialog box can be displayed using a number of different commands: select View➪ Toolbars ➪ Customize, select the Tools ➪ Customize command, or right-click any toolbar and choose Customize. In addition, you can customize toolbars by writ- ing VBA code. Don’t be afraid to experiment with toolbars. If you mess up a built-in toolbar, you can easily reset it to its default state. Just access the Customize dialog box and click the Toolbars tab. Then select the toolbar in the list, and click the Reset button. How Excel Handles Toolbars Before you start working with custom toolbars, it’s important to understand how Excel deals with toolbars in general. You may be surprised. Storing toolbars Toolbars can be attached to XLS (worksheet) or XLA (add-in) files, which makes it easy to distribute custom toolbars with your applications (see “Distributing tool- bars” later in this chapter). You can attach any number of toolbars to a workbook. When the user opens your file, the attached toolbars automatically appear. An exception occurs if a toolbar with the same name already exists. In such a case, the new toolbar does not replace the existing one. Note 4799-2 ch22.F 6/11/01 9:42 AM Page 654 655 Chapter 22 ✦ Creating Custom Toolbars Excel stores toolbar information in an XLB file, which resides in Windows’s main directory (\WINDOWS or \WINNT). The exact name (and location) of this file varies. Why is this XLB file important? Assume that a colleague gives you an Excel work- book that has a custom toolbar stored in it. When you open the workbook, the tool- bar appears. You examine the workbook but decide that you’re not interested in it. Nonetheless, when you exit Excel, the custom toolbar is added to your XLB file. If you make any toolbar changes— from the minor adjustment of a built-in toolbar to the introduction of a custom toolbar— the XLB file is resaved when you exit Excel. Because the entire XLB file is loaded every time you start Excel, the time it takes to start and exit Excel increases significantly as the XLB file grows in size. Plus, all those toolbars eat up memory and system resources. Therefore, it’s in your best interest to delete custom toolbars that you never use. Use the View ➪ Toolbars ➪ Customize command to do this. If you upgrade to Excel 2002 from a previous version, the information in your old XLB file will not be migrated to Excel 2002. In other words, you will lose any tool- bar customizations that you’ve made. When toolbars don’t work correctly Excel’s approach to storing toolbars can cause problems. Suppose you’ve developed an application that uses a custom toolbar, and you’ve attached that toolbar to the application’s workbook. The first time an end user opens the workbook, the toolbar is displayed. When the user closes Excel, your toolbar is saved in the user’s XLB file. If the user alters the toolbar in any way — for example, if he or she accidentally removes a button— the next time your application is opened, the correct toolbar does not appear. Rather, the user sees the altered toolbar, which now lacks an impor- tant button. In other words, a toolbar attached to a workbook is not displayed if the user already has a toolbar with the same name. In many cases, this is not what you want to happen. Fortunately, you can write VBA code to prevent this scenario. The trick is never to allow your custom toolbar to be added to the user’s toolbar collection. The best way to do this is to create the toolbar on the fly every time the workbook is opened and then delete it when your application closes. With this process, the toolbar is never stored in the user’s XLB file. You might think that creating a toolbar on the fly would be a slow process. As you’ll see later in this chapter, creating toolbars with VBA is amazingly fast. Manipulating Toolbars and Buttons Manually Excel makes it easy for you to create new toolbars and modify existing toolbars. In fact, you may not even have to use VBA to work with toolbars, because you can do just about all your toolbar customization without it. Note 4799-2 ch22.F 6/11/01 9:42 AM Page 655 656 Part VI ✦ Developing Applications It’s important to understand that any customizations you make to a toolbar, either built-in or custom, are “permanent.” In other words, the changes remain in effect even when you restart Excel. These toolbar changes are not associated with a par- ticular workbook. To restore a toolbar to its original state, you must reset it. About command bar customization mode To perform any type of manual toolbar (or menu) customization, Excel needs to be in what I call command bar customization mode. You can put Excel into this mode by using any of these techniques: ✦ Select View➪ Toolbars ➪ Customize. ✦ Select Tools➪ Customize. ✦ Right-click any toolbar or menu, and select Customize from the shortcut menu. When Excel is in command bar customization mode, the Customize dialog box is displayed, and you can manipulate toolbars and menus any way you like. You’ll find that you can right-click menus and toolbars to get a handy shortcut menu (see Figure 22-1). After you’ve made your customization, click the Close button in the Customize dialog box. The Customize dialog box includes three tabs: Toolbars Lists all the available toolbars, including custom toolbars you have created. The list box also includes the two menu bars (Worksheet Menu Bar and Chart Menu Bar), plus any other custom menu bars. Commands Lists by category all the available built-in commands. Use this tab to add new items to a toolbar or menu bar. Options Lets you select various options that relate to toolbars and menus. These include icon size, screen tips, and menu animations. The Options tab of the Customize dialog box contains an option called Always show full menus. I strongly recommend that you turn this option on. When this option is off, incomplete menus are displayed. Apparently, Microsoft thought this option would lessen confusion for beginners. In fact, it usually has the opposite effect. In the sections that follow, I briefly describe how to perform some common toolbar modifications manually, using the Customize dialog box. Tip Caution 4799-2 ch22.F 6/11/01 9:42 AM Page 656 657 Chapter 22 ✦ Creating Custom Toolbars Figure 22-1: In command bar customization mode, you can alter all toolbars and menus. Hiding or displaying a toolbar The Toolbars tab in the Customize dialog box displays every toolbar (built-in tool- bars and custom toolbars). Add a check mark to display a toolbar; remove the check mark to hide it. The changes take effect immediately. Creating a new toolbar Click the New button, and then enter a name in the New Toolbar dialog box. Excel creates and displays an empty toolbar. You can then add buttons (or menu com- mands) to the new toolbar. Figure 22-2 shows a custom toolbar that I created manually. This toolbar, called Custom Formatting, contains the formatting tools that I use most frequently. Notice that this toolbar includes drop-down menus as well as standard toolbar buttons. Figure 22-2: A custom toolbar that contains formatting tools 4799-2 ch22.F 6/11/01 9:42 AM Page 657 658 Part VI ✦ Developing Applications Renaming a custom toolbar Select a custom toolbar from the list, and click the Rename button. Enter a new name in the Rename Toolbar dialog box. You cannot rename a built-in toolbar. Deleting a custom toolbar Select a custom toolbar from the list, and click the Delete button. You cannot delete a built-in toolbar. Resetting a built-in toolbar Select a built-in toolbar from the list, and click the Reset button. The toolbar is restored to its default state. If you’ve added any custom tools to the toolbar, they are removed. If you’ve removed any of the default tools, they are restored. The Reset button is disabled when a custom toolbar is selected. Moving and copying controls When Excel is in command bar customization mode (that is, the Customize dialog box is displayed), you can copy and move toolbar controls freely among any visible toolbars. To move a control, drag it to its new location, either within the current toolbar or on a different toolbar. To copy a control, press Ctrl while you drag that control to another toolbar. You can also copy a control within the same toolbar. Inserting a new control To add a new control to a toolbar, use the Commands tab of the Customize dialog box shown in Figure 22-3. Figure 22-3: The Commands tab contains a list of every available built-in control. 4799-2 ch22.F 6/11/01 9:42 AM Page 658 659 Chapter 22 ✦ Creating Custom Toolbars Here, the controls are arranged in 17 categories. When you select a category, the controls in that category appear to the right. To find out what a control does, select it and click the Description button. To add a control to a toolbar, locate it in the Commands list, and then click and drag it to the toolbar. Adding a toolbar button that executes a macro To create a new toolbar button to which you will attach a macro, activate the Commands tab of the Customize dialog box, and then choose Macros from the Categories list. Drag the command labeled Custom Button to your toolbar (by default, this button has a smiley face image). After adding the button, right-click it and select your options from the menu shown in Figure 22-4. You’ll want to change the name, assign a macro, and (I hope) change the image. Selecting Change Button Image from the shortcut menu displays a list of 42 images. This is a tiny subset of all of the available images you can use. See “Adjusting a toolbar button image” later in this chapter. Figure 22-4: Customizing a toolbar button Note 4799-2 ch22.F 6/11/01 9:42 AM Page 659 660 Part VI ✦ Developing Applications Distributing toolbars In this section, I describe how to distribute custom toolbars to others, and I outline what you need to be aware of to prevent problems. Attaching a toolbar to a workbook To store a toolbar in a workbook file, select View➪ Toolbars➪ Customize to display the Customize dialog box. Click the Attach button to bring up the Attach Toolbars dialog box, shown in Figure 22-5. This dialog box lists all the custom toolbars in the Toolbars collection in the list box on the left. Toolbars already stored in the work- book are shown in the list box on the right. Figure 22-5: The Attach Toolbars dialog box To attach a toolbar, select it and click the Copy button. When a toolbar in the right list box is selected, the Copy button reads “Delete”; you can click it to remove a selected toolbar from a workbook. Oddly, there is no way to attach or detach toolbars from a workbook with VBA. These operations must be performed manually. The copy of the toolbar stored in the workbook always reflects its contents at the time you attach it. If you modify the toolbar after attaching it, the changed version is not automatically stored in the workbook. You must manually remove the old toolbar and then attach the edited toolbar. A toolbar that’s attached to a workbook automatically appears when the workbook is opened, unless the workspace already has a toolbar by the same name. See “How Excel Handles Toolbars” earlier in this chapter. Distributing a toolbar with an add-in As I mentioned in Chapter 21, distributing an application as an add-in is often the preferred method for end users. Not surprisingly, an add-in can also include one or more custom toolbars. But you need to be aware of a potential glitch. Caution Note 4799-2 ch22.F 6/11/01 9:42 AM Page 660 661 Chapter 22 ✦ Creating Custom Toolbars Here’s a typical scenario: You create an application that uses a custom toolbar. The buttons on that toolbar execute VBA procedures in the application’s workbook. You attach the toolbar to the workbook and save the workbook. You create an add-in from the workbook. You close the XLS version of the application. You install the add-in. You click a button on the custom toolbar and the XLS file opens! Your intent, of course, is to have the toolbar buttons execute procedures in the add-in, not the XLS file. But when you attach the toolbar to the workbook, the tool- bar is saved in its current state. In that state, the workbook includes references to the macros in the XLS file. Consequently, clicking a button opens the XLS file so that the macro can be executed. You could manually (or via VBA) change the OnAction property of each toolbar button so it refers to the add-in version (the XLA file). A better approach, however, is to write code to create the toolbar on the fly when the add-in is opened. I discuss this topic in detail later in the chapter. Manipulating the CommandBars Collection The CommandBars collection, contained in the Application object, is a collection of all CommandBar objects. Each CommandBar object has a collection of Controls. All these objects have properties and methods that enable you to control toolbars with VBA procedures. In this section, I provide some key background information that you should know about before you start writing code to manipulate toolbars. As always, a thorough understanding of the object model will make your task much easier. You manipulate Excel command bars (including toolbars) by using objects located within the CommandBars collection. This collection consists of the following items: ✦ All 54 of Excel 2002’s built-in toolbars. ✦ Any other custom toolbars that you create. ✦ A built-in menu bar named Worksheet Menu Bar. This appears when a work- sheet is active. ✦ A built-in menu bar named Chart Menu Bar. This appears when a chart sheet is active. ✦ Any other custom menu bars that you create. ✦ All 58 of Excel 2002’s built-in shortcut menus. 4799-2 ch22.F 6/11/01 9:42 AM Page 661 662 Part VI ✦ Developing Applications Command bar types As I mentioned at the beginning of this chapter, there are actually three types of command bars, each of which is distinguished by its Type property. Possible set- tings for the Type property of the CommandBars collection are shown in the follow- ing table. VBA provides built-in constants for the command bar types. Type Description Constant 0 Toolbar msoBarTypeNormal 1 Menu Bar msoBarTypeMenuBar 2 Shortcut Menu msoBarTypePopUp Listing all CommandBar objects If you’re curious about the objects in the CommandBars collection, the following procedure should be enlightening. Executing this procedure generates a list (shown in Figure 22-6) of all CommandBar objects in the CommandBars collection. For Excel 2002, it lists a total of 114 built-in command bars, plus any custom menu bars or toolbars. For each command bar, the procedure lists its Index, Name, Type property settings (displayed as Toolbar, Menu Bar,orShortcut), and whether it’s a built-in command bar. New Toolbars in Excel 2002 If you’re upgrading from a previous version of Excel, you’ll notice several new toolbars. Compared to Excel 2000, Excel 2002 has 13 new toolbars: Borders Drawing Canvas Organization Chart Envelope Protection Clipboard Diagram PivotTable Field List Task Pane Draw Border Insert Shape Text to Speech Online Meeting You might be surprised to learn that the Task Pane is actually a toolbar — a very nonstandard toolbar. It contains a single control. 4799-2 ch22.F 6/11/01 9:42 AM Page 662 663 Chapter 22 ✦ Creating Custom Toolbars Figure 22-6: VBA code produced this list of all CommandBar objects. Sub ShowCommandBarNames() Dim Row As Integer Dim cbar As CommandBar Cells.Clear Row = 1 For Each cbar In CommandBars Cells(Row, 1) = cbar.Index Cells(Row, 2) = cbar.Name Select Case cbar.Type Case msoBarTypeNormal Cells(Row, 3) = “Toolbar” Case msoBarTypeMenuBar Cells(Row, 3) = “Menu Bar” Case msoBarTypePopUp Cells(Row, 3) = “Shortcut” End Select Cells(Row, 4) = cbar.BuiltIn Row = Row + 1 Next cbar End Sub 4799-2 ch22.F 6/11/01 9:42 AM Page 663 [...]... button, the Ctrl+N shortcut key, the Task Pane (in Excel 2002) , or the Desktop shortcut menu to create a new workbook 687 4799-2 ch23.F 688 6/11/01 9:42 AM Page 688 Part VI ✦ Developing Applications Moving Up from Excel 5/95? If you’ve customized menus using Excel 5 or Excel 95, you can pretty much forget everything you ever learned Beginning with Excel 97, menu customization has changed significantly... free-floating An overview of Excel s menu system Types of menu modifications you can make How to manipulate menus with VBA Various menu programming techniques used with events A useful (and very easy) technique for creating custom menus A procedure for replacing standard menu conventions with your own How to customize the shortcut menus 4799-2 ch23.F 686 6/11/01 9:42 AM Page 686 Part VI ✦ Developing Applications... add elements, and change elements In addition, you can temporarily replace Excel s standard menu bar with one of your own creation You can change Excel s menus two ways: manually, or with VBA code When you close Excel, it saves any changes that you’ve made to the menu system, and these changes appear the next time you open Excel The information about menu modifications is stored in an XLB file, usually... Standard, STANDARD, standard, etc Caution Index numbering for command bars is not consistent across versions of Excel! For example, in Excel 2002, the 3-D Settings toolbar has an Index of 58 In Excel 2000, the WordArt toolbar has an Index of 58 If your application must work in different versions of Excel, you should use the Name property instead of the Index property Deleting a command bar To delete a custom... in Excel 5/95 ✦ There is no easy way to assign a VBA macro to a new menu item on the Tools menu This was a piece of cake with Excel 5/95 Later in this chapter, however, I provide VBA code that you can use to add a new menu item to the Tools menu ✦ Excel 2000 and later, by default, displays only the most recently used menu items In my opinion, this is one of the worst ideas Microsoft has come up with. .. CommandBars.Add With TBar Name = “MonthList” Visible = True End With ‘ Add a DropDown control Set NewDD = CommandBars(“MonthList”).Controls.Add _ (Type:=msoControlDropdown) With NewDD Caption = “DateDD” OnAction = “PasteMonth” Style = msoButtonAutomatic ‘ Fill it with For i = 1 To AddItem Next i ListIndex = End With End Sub month name 12 Format(DateSerial(1, i, 1), “mmmm”) 1 4799-2 ch22.F 6/11/01 9:42 AM Page 683 ... msoBarNoMove — Cannot be moved msoBarNoChangeVisible — Its visibility state cannot be changed by the user msoBarNoChangeDock — Cannot be docked to a different position msoBarNoVerticalDock — Cannot be docked along the left or right edge of the window msoBarNoHorizontalDock — Cannot be docked along the top or bottom edge of the window 4799-2 ch22.F 6/11/01 9:42 AM Page 667 Chapter 22 ✦ Creating Custom... VI ✦ Developing Applications What You Can Do with Excel s Menus Typical Excel users get by just fine with the standard menus Because you’re reading this book, however, you’re probably not the typical Excel user You may want to modify menus to make your life easier and to make life easier for the folks who use the spreadsheets that you develop To modify Excel s menus, you can remove elements, add elements,... right-clicking an item displays a context-sensitive shortcut menu containing relevant commands Excel uses both types of menus, and developers have almost complete control over Excel s entire menu system, including shortcut menus This chapter tells you everything you need to know about working with Excel s menus A Few Words about Excel s Menu Bar If you’ve read Chapter 22, you already know that a menu bar (like... Next statement is present to prevent the error message that occurs when certain Excel 2002 toolbars are accessed in the incorrect context Specifically, the toolbars that must be accessed in the correct context are Pivot Table Field List, Online Meeting, and Document Recovery 677 4799-2 ch22.F 6 78 6/11/01 9:42 AM Page 6 78 Part VI ✦ Developing Applications Changing a control’s caption dynamically The . Excel! For example, in Excel 2002, the 3-D Settings toolbar has an Index of 58. In Excel 2000, the WordArt toolbar has an Index of 58. If your application must work in different versions of Excel, . built-in command bar. New Toolbars in Excel 2002 If you’re upgrading from a previous version of Excel, you’ll notice several new toolbars. Compared to Excel 2000, Excel 2002 has 13 new toolbars: Borders. user. msoBarNoChangeDock — Cannot be docked to a different position. msoBarNoVerticalDock — Cannot be docked along the left or right edge of the window. msoBarNoHorizontalDock — Cannot be docked along