View Single Post
  #9   Report Post  
Posted to microsoft.public.excel.programming
Paul James Paul James is offline
external usenet poster
 
Posts: 11
Default saving toolbar buttons on custom toolbar

Greg - again, thank you for all the information. I ran the
TestMacroAssignment() sub and you're right, it did display the workbook name
in the macro assignment designation. But then that code displays the macro
assignment for a menu bar item. Do you know what the code would be to
display the same thing for the toolbar buttons?

The reason I ask is that I opened another workbook that I created some time
ago that has a custom toolbar attached to the workbook. This workbook
doesn't recreate the toolbar when it opens, the toolbar is simply attached
to the workbook.

I renamed that workbook to see if the toolbar would still open when the
workbook opens (it does), and the toolbar buttons also function properly,
even though they're in a workbook with a different name. So it would seem
that the macro assignments on those toolbar buttons are not dependent on the
name of the workbook.

When I close the workbook, I delete the toolbars using
"Application.CommandBars("toolbar name").Delete" in the Auto_Close sub. And
after that workbook is closed, the toolbar no longer appears in the list of
toolbars in the application.

These observations seem to suggest that the toolbar goes with the workbook.
I think one of the key steps might be that you need to place all of the
buttons and macro assignments in the toolbar before you Attach it to the
workbook. But it seems to work well, even after you rename the workbook,
without having to rebuild the toolbar every time you open the workbook.

Try it yourself as an experiment, following these steps:

- create a Sub procedure
- create a new toolbar, place a new button on the toolbar and assign that
sub procedure to the button
- before you close the Customize dialog, go back to the Toolbars tab and
Attach the new toolbar to the workbook.
- add this sub to a module in the workbook:
Sub Auto_Close()
On Error Resume Next
Application.CommandBars("toolbar name").Delete
End Sub
- try out the toolbar button to confirm that it's calling the sub
- save the workbook, close then reopen, then save it with another name
- open the renamed workbook and you'll see that the custom toolbar is still
there

You'll also notice that after you close the workbook, the custom toolbar no
longer appears in the list of toolbars in the application. It's only there
when the workbook that contains the "attached" toolbar is open.

In view of the foregoing, it seems to me that we don't need to recreate
custom toolbars in VBA code in order to use them with a workbook. Unless
there's something I'm missing.

Paul