View Single Post
 
Old 06-25-2024, 05:27 PM
Guessed's Avatar
Guessed Guessed is offline Windows 10 Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,166
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

I think you have a few options to determine the best approach.

All keybindings have a customisation context (which means the shortcut resides in a document, template or addin). So it shouldn't be difficult to enable/disable a template to change active keybindings. For instance, I would put your specific keybindings into an addin and then disable that addin to disable the keybindings.

You could also add a GetKeyState to each of your macros so that when Ctrl, Shift or Alt is held down, an alternative path through your macro is taken (ie, do the built-in action instead)

Another option might be to have a toggle switch on your ribbon that changes your keybindings. Sample code to change keybindings can be found here How to create hotKey in Word VBA (programatically)? - Stack Overflow

Code that might help you along your path is below
Code:
Sub GetBindings()
  Dim aKB As KeyBinding
  CustomizationContext = ActiveDocument.AttachedTemplate      'or ThisDocument
  For Each aKB In KeyBindings
    Debug.Print aKB.KeyString, aKB.Command    ', aKB.CommandParameter, aKB.KeyCategory
    'If aKB.KeyString = "Alt+B" Then aKB.Clear
    ''things you might want to explore
'    aKB.Disable    '' equivalent to clicking the Remove button in the Customize Keyboard dialog box
'    aKB.Clear      ''reset a built-in command to its default key assignment
'    aKB.Rebind wdKeyCategoryCommand, "TestKeybinding"
'    aKB.Execute
  Next aKB
End Sub

'---------------------------------------------
Sub AddKeyBinding()
  With Application
    .CustomizationContext = ThisDocument
    .KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyAlt, wdKey0), KeyCategory:=wdKeyCategoryCommand, Command:="TestKeybinding"
  End With
End Sub

'---------------------------------------------
'Sub TestKeybinding()      ' \\ Test sub for keybinding
'  MsgBox "We have a winner", vbInformation, "Success"
'End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia

Last edited by Guessed; 06-25-2024 at 11:42 PM.
Reply With Quote