Consulting

Results 1 to 6 of 6

Thread: add an item to a vbe toolbar

  1. #1
    VBAX Mentor
    Joined
    Jun 2005
    Posts
    374
    Location

    add an item to a vbe toolbar

    hello
    i am trying to add an item to the vbe edit toolbar.
    [VBA]
    Sub cBox()
    With Application.VBE.CommandBars("edit").Controls.Add(before:=2)
    .Caption = "goto last module"
    .OnAction = "entervba"
    .BeginGroup = True
    End With
    End Sub

    [/VBA]
    what am i doing wrong?
    moshe

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    What is the problem as far as you see it? Does the called macro not work?
    Last edited by Bob Phillips; 03-05-2007 at 05:25 PM.

  3. #3
    VBAX Mentor
    Joined
    Jun 2005
    Posts
    374
    Location
    hello
    i do not get an error message but the onaction method do not seem to work
    moshe

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    That is what I thought.

    This is tricky so watch closely.

    First, the bad news is that the OnAction property is ignored in the VBE. The good news is that we have a specific commandbars event handler that we can use.

    To utilise this functionlity, you will need to set a reference to the "Microsoft Visual Basic For Applications Extensibility" library in Tools>references.

    Start by creating a class module, call it CBarEvents, and add this code.

    [vba]

    Option Explicit

    Public WithEvents oCBControlEvents As CommandBarEvents

    Private Sub oCBControlEvents_Click(ByVal cbCommandBarControl As Object, _
    Handled As Boolean, _
    CancelDefault As Boolean)

    On Error Resume Next
    'Run the routine given by the commandbar control's OnAction property
    Application.Run cbCommandBarControl.OnAction

    Handled = True
    CancelDefault = True

    End Sub
    [/vba]
    This will take the button click, and route it through whatever OnAction macro you declared.

    When you create your menu item, you need to attach it to the class, creating a event c ollection item to manage it. In your standard module use this adapted version of your code

    [vba]

    Option Explicit

    Dim mcolBarEvents As New Collection 'collection to store menu item click event handlers

    Sub BrandNewBarAndButton()
    Dim CBE As CBarEvents
    Dim myBar As CommandBar
    Dim myControl

    On Error Resume Next
    Application.VBE.CommandBars("Extra").Delete
    On Error GoTo 0

    Set myBar = Application.VBE.CommandBars.Add("Extra", , False, True)
    myBar.Visible = True
    Set myControl = myBar.Controls.Add(msoControlButton, , , 1)
    With myControl
    .Caption = "myVBEButton"
    'Other properties set
    .FaceId = 29
    .OnAction = "LittleClick"
    End With

    'Create a new instance of our button event-handling class
    Set CBE = New CBarEvents

    'Tell the class to hook into the events for this button
    Set CBE.oCBControlEvents = Application.VBE.Events.CommandBarEvents(myControl)

    'And add the event handler to our collection of handlers
    mcolBarEvents.Add CBE

    End Sub
    [/vba]
    When you click the toolbar item now, it will run the LittleClick macro.

  5. #5
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Quote Originally Posted by xld
    ...First, the bad news is that the OnAction property is ignored in the VBE. The good news is that we have a specific commandbars event handler that we can use....
    Just a quick thank-you Bob. I was going daffy (well... I may still be daffy, but that's not at point) trying to beat OnAction into submission.
    This was a nice find!

    Mark

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Glad it was helpful Mark.

    I have noticed that attachments get deleted after a while, so posting the code inline seems to be the way to go, it makes the archives more useful, as it did for you here.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •