VBA Add Shortcut Keys to Dynamic Menu on the Userform

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

Who is Participating?
danaseamanConnect With a Mentor Commented:
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.

         ' 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
End Function

Open in new window

Append it to the Caption separated by vbTab and it should be recognized as a Shortcut.
"New" & vbTab & "Ctrl+N"
eyeweyewAuthor Commented:

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.
eyeweyewAuthor Commented:

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!
For subclassing you can use the one from VbAccelerator.com

SubClassing With The SSubTmr6 DLL

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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.