18.11.2014 Views

Microsoft Office

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Creating Custom Excel Add-Ins 45<br />

Modify the User Interface the Old Way<br />

Before Excel 2007, it was much simpler to provide a way to execute a macro in an add-in. It was done<br />

using aCommandBar objects, which display menus and toolbars. Excel 2007 still supports CommandBars<br />

for compatibility. All changes made to a CommandBar object are displayed in the Menu Commands or<br />

Custom Toolbars groups in the Add-Ins tab.<br />

If you would like to modify change case.xlsm to use CommandBars, add these two procedures to the<br />

ThisWorkbook code module:<br />

Private Sub Workbook_Open()<br />

Set NewMenuItem = Application.CommandBars(“Worksheet Menu Bar”) _<br />

.Controls(“Tools”).Controls.Add<br />

With NewMenuItem<br />

.Caption = “Change Case of Text...”<br />

.BeginGroup = True<br />

.OnAction = “ChangeCaseofText”<br />

End With<br />

End Sub<br />

Private Sub Workbook_BeforeClose(Cancel As Boolean)<br />

On Error Resume Next<br />

Application.CommandBars(“Worksheet Menu Bar”).Controls(“Tools”). _<br />

Controls(“Change Case of Text...”).Delete<br />

End Sub<br />

When the add-in is opened, this code adds a new control to the Menu Commands group of the Add-Ins tab.<br />

When it’s closed, the new control is removed. Simple and easy. It’s too bad <strong>Microsoft</strong> couldn’t use such a simple<br />

approach for customizing the Ribbon.<br />

Figure 45.7 shows the new command in the Ribbon. Click this button, and the ChangeCaseofText macro<br />

executes.<br />

FIGURE 45.7<br />

The new user interface control in the Ribbon.<br />

781

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!