Solved

VBA Add Shortcut Keys to Dynamic Menu on the Userform

Posted on 2009-07-01
6
395 Views
Last Modified: 2012-05-11
Does anyone know how to create shortcut Keys to the API dynamic menu on the Userform? For example: I have a item "New" on the menu and I want to assign the shortcut Ctrl+N to it and "Ctrl+N" will sit beside "New" on the menu.

I have attached my file

Thanks
API-Custom-Menu.xls
0
Comment
Question by:eyeweyew
  • 3
  • 2
6 Comments
 
LVL 22

Expert Comment

by:danaseaman
ID: 24762502
Append it to the Caption separated by vbTab and it should be recognized as a Shortcut.
"New" & vbTab & "Ctrl+N"
0
 

Author Comment

by:eyeweyew
ID: 24765749
danaseaman,

Ok, so now it aligns properly on the menu. Thanks for that!

But how can I assign the the actual short key so when the user press Ctrl with N, it will call a certain Sub.
0
 
LVL 22

Accepted Solution

by:
danaseaman earned 500 total points
ID: 24766028
You need to store the accelerators somewhere and subclass WM_MENUCHAR message of your Menu.
This is just an example and will have to be modified depending on which subclass code you implement.



    Case WM_MENUCHAR
         ' Check that this is my menu:
         lFlag = wParam \ &H10000
         If ((lFlag And MF_SYSMENU) <> MF_SYSMENU) Then
            hMenu = lParam
            iChar = (wParam And &HFFFF&)
            Debug.Print "WM_MENUCHAR", hMenu, Chr$(iChar)
            ' See if this corresponds to an accelerator on the menu:
            lR = pvParseMenuChar(hMenu, iChar)
            If lR > 0 Then
               bHandled = True
               lReturn = lR
            End If
         End If
         If Not bHandled Then
            'ISubclass_WindowProc = CallOldWindowProc(hWnd, iMsg, wParam, lParam)
            lReturn = ssc_CallOrigWndProc(lng_hWnd, uMsg, wParam, lParam)
         End If
 
Private Function pvParseMenuChar( _
   ByVal hMenu As Long, _
   ByVal iChar As Long) As Long
 
   Dim lPos             As Long
   Dim i                As Long
   Dim iModifier        As Long
 
   iModifier = GetShiftMask
   Debug.Print "iChar Before", iChar
   If iChar > 96 Then
      iChar = iChar - 32 'LCase to UCase
   ElseIf iChar < 65 Then
      iChar = iChar + 64 'Ctrl to Ucase
   End If
 
   Debug.Print "iChar After", iChar
 
   For i = 0 To UBound(MenuID)
      If MenuID(i).hMenu = hMenu Then
         If MenuID(i).ShortcutKey = iChar Then
            If MenuID(i).ShortcutModifier = iModifier Then
               pvParseMenuChar = &H20000 + MenuID(i).Position - 1
               Exit Function
            End If
         End If
      End If
   Next
 
End Function

Open in new window

0
 

Author Comment

by:eyeweyew
ID: 24766933
danaseaman:

Don't really understand how to implement this. Could you provide me a simple macro which has this in it? So I can try to figure out how it works.

Your help would be appreciated!
0
 
LVL 22

Expert Comment

by:danaseaman
ID: 24767055
For subclassing you can use the one from VbAccelerator.com

SubClassing With The SSubTmr6 DLL
http://www.cpearson.com/excel/SubclassingWithSSubTmr6.htm 

When Popmenu is created you will need to store the handle and shortcuts in an array or collection.
I store the Ascii character value, Shift state, and menu item position which is later searched when you receive msg WM_MENUCHAR.
Note: GetShiftMask gets current kybd shift state and that is compared to shift state of your accelerator.

I don't have plug-in code that will work in Excel.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question