William has a requirement to delete a menu item when it’s clicked. If you try to do this in a straight forward way, you’ll get a Method ‘Delete’ of object ‘_CommandBarButton’ failed because you can’t delete the button in the same procedure that was called by the button. The best way I could think to solve this was to tag the button for deletion and run another procedure an instant later that did the dirty business.
The first sub creates a button under the Tools menu which runs the DeleteMyself sub. When the button is clicked, the Tag property is changed and DeleteLater is scheduled to run right after execution of the current procedure is complete. DeleteLater find the control with the special tag and deletes it.
.Caption = “Clicktodelete”
.OnAction = “DeleteMyself”
.Visible = True
.Controls(“Clicktodelete”).Tag = “DELME”
Application.OnTime Now, “DeleteLater”
Dim Cbc As CommandBarButton
Set Cbc = Application.CommandBars.FindControl(, , “DELME”)
If Not Cbc Is Nothing Then