Adding & removing menu buttons
Sub Tester1()
On Error Resume Next
Set cBar = CommandBars("Worksheet Menu Bar")
Do
cBar.Controls("Box Units").Delete
cBar.Controls("Clear Sheets").Delete
Set ctl = Nothing: Set ctl1 = Nothing
Set ctl = cBar.Controls("Box Units")
Set ctl1 = cBar.Controls("Clear Sheets")
Loop Until ctl Is Nothing And ctl1 Is Nothing
On Error GoTo 0
With CommandBars("Worksheet Menu Bar").Controls.Add
.Caption = "Box Units"
.Style = msoButtonCaption
.OnAction = "NameBoxes"
End With
With CommandBars("Worksheet Menu Bar").Controls.Add
.Caption = "Clear Sheets"
.Style = msoButtonCaption
.OnAction = "ClearAll"
End With
End Sub
BeforeClose will be much more complicated. You will need to check if the
controls exist and which workbook their onAction property points to. Then
if it is to this workbook, check if there are any other copies of this
workbook open and if so reassign the existing controls to point to code in
one of those workbooks - or if not, then delete the controls.
The modification assumes the code executed by each button (onaction macro)
is general in operation and doesn't work just on the workbook that created
the controls. If this is not true, you would have to make the onaction code
more general since only one set of buttons will exist at any one time.
--
Regards,
Tom Ogilvy
"R. Choate" wrote in message
...
I have code in my "This Workbook" module to add 2 buttons to the menu bar.
Here is the current code for that:
With CommandBars("Worksheet Menu Bar").Controls.Add
.Caption = "Box Units"
.Style = msoButtonCaption
.OnAction = "NameBoxes"
End With
With CommandBars("Worksheet Menu Bar").Controls.Add
.Caption = "Clear Sheets"
.Style = msoButtonCaption
.OnAction = "ClearAll"
End With
I need to replace this with better code AND add code to my BeforeClose
event to delete the buttons. The kicker is that there might
be several copies of each button if the user has opened more than one copy
of the file or has opened it multiple times. I need to
have the code delete all possible instances (copies) of each of these 2
buttons. Any good ideas?
--
RMC,CPA
|