PDA

View Full Version : CommandButton corrupts custom menus?



lbenve
03-17-2011, 10:02 AM
Hi,

I'm a VBA newbie and I'm facing an issue while trying to programmatically add a CommandButton to a document. I need to add some buttons here and there in a document, each with a specific purpose; to do this I've added some CommandBarButtons to Application.CommandBars("Menu Bar") and, for each buttons, I've created an event handler that adds a button:

Private Sub addFirstBtn_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
Dim shp As InlineShape
Set shp = Selection.InlineShapes.AddOLEControl(ClassType:="Forms.CommandButton.1")
shp.OLEFormat.Object.Name = "firstButtonName"
shp.OLEFormat.Object.Caption = "Perform check"
End Sub

(the "click" event of the dinamically created button is handled by a firstButtonName_Click routine)

This works just for the first time; after that the CommandBarButtons stop firing events. The same happens if I add a button manually (in Office 2007: Developer > Legacy Tools > Command Button). In other words it seems that the CommandButton is somehow corrupting the CommandBarButtons. Ideas?

Thank you.