Private Sub Workbook_Activate()
Dim _
CBar As CommandBar, _
ctl As CommandBarControl, _
ctlSub As CommandBarControl, _
btnCustDel As CommandBarButton, _
btn As CommandBarButton, _
ctlEdit As CommandBarPopup, _
iIndex As Integer, _
bolBail As Boolean
'// As we are just rotating thru the command buttons on the Ply shortcut//
'// menu, we can do this straightaway. //
For Each ctl In Application.CommandBars("Ply").Controls
With ctl
'// You'll of course want to start reading the topics in help, //
'// but basically .ID is for BuiltIn commands, and controls what//
'// happens when the button is pushed (like OnAction) for the //
'// factory buttons. So... we'll use it to get the right //
'// button, and find the button's index, so we can stick the //
'// custom bttn in the same place visually. //
If .ID = 847 Then
iIndex = .Index
.Visible = False
Exit For
End If
End With
Next
'// Now we'll set a reference to the commandbar and add a control to it.//
'// There are different types, we are "replacing" w/the same type to be //
'// "sneaky". We're sticking it right Before the now hidden control, //
'// and the Temporary, while really unnecessary (as we delete the bttn //
'// ea time the wb deactivates) is just a safety I choose to use, as //
'// even if my code goes Thunk(!), Excel will kill the button when the //
'// app is quit. //
Set CBar = Application.CommandBars("Ply")
Set btnCustDel = CBar.Controls.Add(Type:=msoControlButton, _
Before:=iIndex, _
Temporary:=True)
'// Pretty self-explanatory; I use .Tag as (IMHO) an effective way to //
'// find the button later to kill it. //
With btnCustDel
.Caption = "&Delete"
.OnAction = "MyMacro"
.Tag = "SubDel"
End With
If Application.Version <= 11 Then
'// Since we're actually looking for sub commands under "Edit", first//
'// we'll set a reference to the CB... //
Set CBar = Application.CommandBars("Worksheet Menu bar")
'// ...then loop thru the controls on the MenuBar, find "Edit"... //
For Each ctlEdit In CBar.Controls
If ctlEdit.Caption = "&Edit" Then
'// ...loop thru the controls under "Edit" to find "Delete //
'// Sheet", again using the .ID //
For Each ctlSub In ctlEdit.Controls
If ctlSub.ID = 847 Then
iIndex = ctlSub.Index
ctlSub.Visible = False
'// Since we've 'drilled down' to the control, we'll//
'// stick our bttn building here... //
Set btnCustDel = ctlEdit.Controls.Add( _
Type:=msoControlButton, _
Before:=iIndex, _
Temporary:=True _
)
With btnCustDel
.Caption = "&Delete Sheet"
.OnAction = "MyMacro"
.Tag = "SubDel"
End With
'// Just to exit the outer loop, since we're done //
bolBail = True
Exit For
End If
Next
End If
If bolBail Then Exit For
Next
End If
End Sub
Private Sub Workbook_Deactivate()
Dim _
ctl As CommandBarControl, _
ctlEdit As CommandBarPopup, _
ctlSub As CommandBarControl, _
CBar As CommandBar
'// While I try and avoid blinding the code to errors, just included in //
'// case the button doesn't exist (which shouldn't really happen, so //
'// probably a bit sloppy on my part), in which case .Delete will botch //
'// up a perfectly good day... //
On Error Resume Next
For Each ctl In Application.CommandBars("Ply").Controls
With ctl
'// Use the .Tag to Delete //
If .Tag = "SubDel" Then
.Delete
End If
'// and the .ID to bring back the Builtin control //
If .ID = 847 Then
.Visible = True
End If
End With
Next
If Application.Version <= 11 Then
Set CBar = Application.CommandBars("Worksheet Menu bar")
For Each ctlEdit In CBar.Controls
If ctlEdit.Caption = "&Edit" Then
For Each ctlSub In ctlEdit.Controls
If ctlSub.ID = 847 Then
ctlSub.Visible = True
End If
If ctlSub.Tag = "SubDel" Then
ctlSub.Delete
End If
Next
End If
Next
End If
End Sub