Creating custom CommandBarControl objects for a shortcut menu

emorris1000

Registered User.
Local time
Today, 10:39
Joined
Feb 22, 2011
Messages
125
Howdy

I finally got tired of having 100 macros managing my different custom shortcut menus, and decided to figure out how to generate the shortcut menus programatically (because lets be honest, Macros are the devil.)

I found a good tutorial here that I suggest anyone trying to do this should read:

http://blogs.office.com/b/microsoft...t-menu-for-a-form-form-control-or-report.aspx


and was able to create some basic shortcut menus like the following:

Code:
Sub CreateCopyShortcutMenu()
Dim cmbshortcutmenu As Office.CommandBar
 
    Set cmbshortcutmenu = CommandBars.Add("CopyShortcutMenu", _
                        msoBarPopup, False, False)
 
    'ID 19 adds copy command
    cmbshortcutmenu.Controls.Add Type:=msoControlButton, Id:=19

End Sub

Nice and simple, now I have a copy command. But the problem is that I also need some custom commands. Most (possibly all?) of these would be function calls.

Anyone know how to do this?
 

MarkK

bit cruncher
Local time
Today, 10:39
Joined
Mar 17, 2004
Messages
8,187
You want to set the OnAction property of the CommandBarControl object to the name of public function (maybe a sub would work) in a standard module, and then that routine is run when the control is clicked.

Consider code like this, where CustomPopup is a previously defined CommandBar. . .
Code:
   Set cbc = CustomPopup.Controls.Add(msoControlButton)
   With cbc
      .Caption = "Show Wizard . . ."
      .Style = msoButtonIconAndCaption
      .OnAction = "ShowMyWizard"
      .FaceId = 625
      .BeginGroup = True
   End With
. . . so when you click that control, this routine would run . . .
Code:
Public Function ShowMyWizard()
   DoCmd.OpenForm "MyWizard"
End Function
hth
 

emorris1000

Registered User.
Local time
Today, 10:39
Joined
Feb 22, 2011
Messages
125
Ok, that works great! Thanks much. I'll have to check out the late binding method at home. I can't download stuff at work.

Quick followup. Does it have to be a sub or could I just write out the code I wanted executed? Some of them may be pretty simple and wouldn't really justify a sub (I like to keep code minimal/clean.)

ed: for exapmle I tried

.onaction = "msgbox Application.CurrentObjectName" and that didn't work.
 
Last edited:

Users who are viewing this thread

Top Bottom