-1

I've seen similar questions asked, but did not manage to find an answer to my specific problem.

I have in place a custom ribbon tab, with custom buttons. Their purpose is to each insert a QuickPart in the document.

As I have a lot of different QuickParts to be inserted, I didn't want to create a macro for each and every one.

So, when a user select an item in the ribbon, a single macro is called on, which uses the control.ID property to insert the appropriate QuickPart.

Everything works well, but now I'm asked to add keyboard shortcuts for some of these ribbon items.

Here's what I have until now:

  1. The macro that inserts QuickParts:

    Sub TalkToRibbon(control As IRibbonControl)    
        ...    
    End Sub
    
  2. When a shortcut is pressed, this macro is triggered:

    Sub interac_obj_cart()    
        shortcut = "interac_obj_cart"
        TalkToRibbon(shortcut)
    End Sub
    

Whether I define "shortcut" as a String or as a IRibbonControl, I get error messages (albeit different). I feel like there is something I am missing. Thanks in advance for your help

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
EBassal
  • 105
  • 3
  • If you have designed an external shortcut system, disconnected from the ribbon, then in your `interac_obj_cart` you need to locate the respective control from the ribbon and pass it to `TalkToRibbon`. `"interac_obj_cart"` is not a control from the ribbon. – GSerg May 15 '22 at 18:35
  • @GSerg I should have mentioned that « interac_obj_cart » is both the control name from the ribbon and the name of the QuickPart. – EBassal May 15 '22 at 18:39
  • 1
    It doesn't matter, it's a string, not an object. See https://stackoverflow.com/a/62344650/11683. – GSerg May 15 '22 at 18:41
  • @GSerg Thank you for your time, could you give a bit more detail as to how you would resolve this problem? I’m not sure as to how to proceed now. – EBassal May 15 '22 at 20:11

2 Answers2

2

Use keytips for ribbon controls instead of inventing something new. KeyTips are the keyboard shortcuts that appear on the Ribbon when you press the ALT key. You can assign your own KeyTips by using the keytip and getKeytip attributes. The getKeyTip callback has the following signature:

C#: string GetKeytip(IRibbonControl control)

VBA: Sub GetKeytip (control As IRibbonControl, ByRef label)

C++: HRESULT GetKeytip ([in] IRibbonControl *pControl, [out, retval] BSTR *pbstrKeytip)

Visual Basic: Function GetKeytip (control As IRibbonControl) As String

The KeyTip is displayed when the user presses the ALT key plus one to three letters.

Otherwise you need to refactor your code by extracting the handler into a separate function which accepts a controls ID and which can be called by others passing a control ID. For example:

Sub TalkToRibbon(control As IRibbonControl)    
    Call EventHandlerWhichAcceptsID  control.ID   
End Sub

And then your keyboard shortcut handler could call it as well.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • Hello, thank you for your time. I would love to be able to use those, as I could simply input the keytips in the custom UI editor I’m using. But unfortunately I’m asked for specific keystrokes for compatibility with other apps in use. I’m asked for ctrl+alt+1, that sort of thing. Cheers – EBassal May 15 '22 at 21:38
  • Refactor your code a bit by extracting the code into a separate function/method. It may accept a control ID as a parameter. – Eugene Astafiev May 15 '22 at 21:43
  • Followed your last suggestion of splitting the code into two subs. If the user inputs a keystroke (ctrl+...), it goes directly to a macro that accepts strings as input, and which inserts the building blocks. If the user clicks on the ribbon icons, then a very short macro is used, which takes the control.ID and passes it as a string to the other macro that inserts building blocks mentionned above. Thanks a lot for your help. – EBassal May 16 '22 at 13:51
0

Why not simply attach keyboard shortcuts to the building blocks or macros, themselves?

I am assuming that by quick parts you are referring to building blocks, perhaps in the quick parts gallery. You can assign keyboard shortcuts to these directly and store them in the same template that holds the building blocks. You can also assign keyboard shortcuts to macros.

The building blocks, macros, and shortcuts should not be stored in the Building Blocks.dotx template but in either a document or a global template.

If you need more on these concepts, here are links to my writing on:

This really has nothing to do with their placement on your custom ribbon.

You can also place a custom building blocks gallery menu on your ribbon.

Here is Greg Maxey's page on building blocks gallery controls. These can also be placed on the QAT.

Charles Kenyon
  • 869
  • 1
  • 8
  • 19
  • Hello, well of course that would solve the issue, but the problem is the macro doesn’t just insert a building block (thanks for the correction), it does a number of others steps prior to inserting. – EBassal May 16 '22 at 03:40
  • See my revised answer. Both building blocks and macros can have keyboard shortcuts directly assigned to them. This does not depend on their placement on the Ribbon. – Charles Kenyon May 16 '22 at 15:14
  • Thanks for your time. I have 40+ building blocks stored in a dotm. I cannot simply assign keyboard shortcuts directly to the building blocks, because there are some steps that have to be done via macro prior to inserting. I cannot assign a keyboard shortcut directly to said macro because there is only one macro for 40+ building blocks. I am sorry if my original explanation was not clear, and am very grateful for your time. Best – EBassal May 16 '22 at 15:32