how to catch menu and shortcut key events in excel-vba.

S

sanji1970

I'm able to handle the commandbar button events by implimenting
CommandBarButton_click() handler. But I'm not able to find the way to
catch menu and ShortCut key events for VBA menu.

Example: suppose user pressed (F5) or selected the menu item
VBA-Run-RunSub/UserFrom, then how can catch this event? please help.
 
C

Cindy Meister

Well, a menu is just another command bar, and the control that executes a
command is a commandbarbutton, so you should be able to handle that same as
for any other toolbar.

Keyboard shortcuts, however, have no exposed event interface you can trap
from outside Word. You can loop the keyboard assignments, and lock out
(disable) them or reassign them, using the KeyBindings collection. But what
you cannot do is assign them to any action/code that's outside of Words own
VBA project. At the very least, you'd need code IN Word that does a callback
to an ADDIN DLL.

-- Cindy
 
M

Mike Walker [MVP]

Hi Sanji

You can configure in Word Keyboard settings to assign a key combination to a
VBA Macro which in turn can run through to a ComAddin method by exposing
this back through the comaddins collection object reference that can expose
methods to cross the boundries. I noticed you referred to Excel which I have
just tried to check supports the keyboard assignment and it doesn't seem to.

Another solution we have done is create a Keyboard Hook that uses a callback
as Cindy refers to but this is quite highlevel and you dont say what
language you are using and or technology such as VSTO, Addins in VB, VB.NET
C#

Regards
Mike Walker MVP
Visual Developer VSTO
 
M

Mike Walker [MVP]

I have just noticed when you record a VBA macro in excel you can shortcut
key to CTRL + x x being the only option which is not that flexible but maybe
enough

Mike Walker MVP
Visual Developer VSTO
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top