What is the difference between CommandBarEvents.Click and CommandBarButton.Click in VBE?

The VBA and VB6 object models complement the CommandBarEvents object, which has a Click event , and an event signature:

Dim WithEvents CmdBarEvents As CommandBarEvents

Private Sub CmdBarEvents_Click(ByVal CommandBarControl As Object, handled As Boolean, CancelDefault As Boolean)

And a reference to CommandBarControl is passed to VBE.Events.CommandBarEvents to register an event handler for this CommaneBarControl :

Set CmdBarEvents = Application.VBE.Events.CommandBarEvents(CmdBarItem)

The Office object model defines separate CommandBar controls that have their own Click events; adding event handlers to VBE CommandBars use CommandBarEvents .

Carlos Quintero of MZ Tools has surprisingly useful, but in this case, slightly conflicting information on his site. It assumes that the CommandBarEvents Approach was used in the old Microsoft Visual Basic 5.0 / 6.0 environment , but also uses the CommandBarControl.Click approach on this page

Is there anything special about VBE and attaching events to CommandBar controls? Are there any problems (memory leaks, problems with IDTExtensibility2 stopping, etc.) if I prefer to use CommandBarControl events for CommandBarEvents events?

+5
source share
1 answer
  • VB6, which has been an IDE since 1998 (and VB5 since 1997), should always use CommandBarEvents. The Office command panels they use are derived from Office 97, in whose CommandBarButton there is no Click event.

  • Office 2000 introduced the Click event in the CommandBarButton class and should always be used to target the Office 2000 and later VBA add-ins (or other VBA hosts) because, although CommandBarEvents seems to be available, Office 2010 64-bit does not support CommandBarEvents (it crashes using).

  • Visual Studio is another animal with its own characteristics.

+7
source

Source: https://habr.com/ru/post/1261035/


All Articles