![]() |
#1
|
|||
|
|||
![]()
I have several macro enabled templates. They are constructed from a file created from another application and have variable text. They also have consistent sets of brackets containing varying text in brackets, depending the the requirement of the template. For example:
template 1.dotm: abc [def] ghi. [JKL] mno [pqr] template 2.dotm: 123abc [def456] ghi789. [222JKL] 345mno [And so on and so forth pqr] They all have 6 custom macros, each one is assigned to a different number keypad key (end, arrow down, pgdn etc.). These keys were picked for rapid editing through the documents. The macros are: 1. select next set of brackets in the document - - assigned to numeric keypad end key 2. edit selected text by removing brackets surrounding selected text then calling macro 1 - - assigned to numeric keypad down arrow key 3. deleting the text along with the brackets then calling macro 1 - - assigned to pgdwn key 4. remove a line completely 5. and 6. other similar fast editing macros 7. assign the macro to the 6 keys 8. toggle back to the default function of the 36 Sometimes I need to revert to the default function to use the number keypad keys as designed (default Word actions), then immediately reassign the keys to the macros. I have tried many code variants, including user forms, to accomplish this. I have also tried assigning the macros to shft+ctl+m to enable the end, arrow down, pgdn etc keys and shft+ctl+b keys to return them to default action. No matter how I go about it I have not succeded. On occasion, the code I've tried is long, even though it should be relatively simple. I normally include the code in a combined module but have tried separating the functions into separate modules. I've also tried enabling and disabling the keys with Autohotkey with and without vbs files to run the macros. Nothing I have tried works. Any suggestions are appreciated. savvy |
#2
|
||||
|
||||
![]()
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. |
#3
|
|||
|
|||
![]()
Thank you so much. I am encountering errors beyond my scope.
Do you contract your time? If yes, what is your rate. Can I email you my mt.dotm, code, and objectives. If not, do you know someone who does? This is my code so far: "'--------------------------------------------- '1 1 1 1 ' Error handling and logging subroutine Sub LogError(errorMessage As String, lineNumber As Integer) Debug.Print "Error " & lineNumber & ": " & errorMessage End Sub '--------------------------------------------- '2 2 2 2 ' Subroutine to clear existing key binding for Ctrl+Shift+End if it exists Sub ClearExistingKeyBinding() On Error Resume Next Application.KeyBindings.Item(BuildKeyCode(wdKeyCon trol, wdKeyShift, wdKeyEnd)).Clear On Error GoTo 0 End Sub '--------------------------------------------- '3 3 3 3 ' Subroutine to add a new key binding for Ctrl+Shift+End Sub AddKeyBinding() On Error Resume Next With Application .CustomizationContext = ThisDocument ' Clear existing key binding (if necessary) ClearExistingKeyBinding ' Attempt to add key binding for Ctrl+Shift+End .KeyBindings.Add keyCode:=BuildKeyCode(wdKeyControl, wdKeyShift, wdKeyEnd), _ keyCategory:=wdKeyCategoryCommand, _ Command:="EndKeybinding" ' Check if an error occurred If Err.Number <> 0 Then ' Log the error LogError "Error adding key binding: " & Err.Description, 2 Err.Clear Else ' Key binding added successfully Debug.Print "Key binding added: Ctrl+Shift+End" End If End With On Error GoTo 0 End Sub '--------------------------------------------- '4 4 4 4 ' Function to build key codes for various combinations Function BuildKeyCode(ParamArray Keys() As Variant) As Long Dim key As Variant For Each key In Keys BuildKeyCode = BuildKeyCode + key Next key End Function '--------------------------------------------- '5 5 5 5 ' Test sub for key binding Sub TestKeybinding() MsgBox "TestKeybinding activated!", vbInformation, "Key Binding Test" End Sub '--------------------------------------------- '6 6 6 6 ' Subroutine to enumerate and list available key bindings Sub ListAvailableKeyBindings() Debug.Print "Starting ListAvailableKeyBindings" Dim aKB As KeyBinding CustomizationContext = ThisDocument ' Loop through each key binding and print details For Each aKB In KeyBindings Debug.Print "Key String: " & aKB.keyString Debug.Print "Command: " & aKB.Command Debug.Print "---" Next aKB Debug.Print "Ending ListAvailableKeyBindings" End Sub '--------------------------------------------- '6 6 6 6 " I want to reassign the numeric pad keys to run simple macros in mt.dotm. However, if I need to manually edit, I need a hot key ex: shft ctl f11 to toggle the numeric pad keys to default word actions and then back. This master template 'mt' will be adapted to create docx for different scenarios, all needing the toggling of the numeric keys functions. I use office 365 and can code a bit with ahk, macroexpress3, and vbs. Appreciatively Mike |
#4
|
|||
|
|||
![]()
oops '---------------------------------------------
'7 7 7 7 ' Subroutine to enumerate and list available key bindings Sub GetBindings() Debug.Print "Starting ListAvailableKeyBindings" Dim aKB As KeyBinding CustomizationContext = ActiveDocument.AttachedTemplate ' Use ThisDocument or AttachedTemplate ' Loop through each key binding and print details For Each aKB In KeyBindings Debug.Print "Key String: " & aKB.keyString Debug.Print "Command: " & aKB.Command ' Additional options to explore ' Debug.Print "CommandParameter: " & aKB.CommandParameter ' Debug.Print "KeyCategory: " & aKB.KeyCategory ' Uncomment and use these lines as needed: ' aKB.Disable ' Equivalent to clicking the Remove button in the Customize Keyboard dialog box ' aKB.Clear ' Resets a built-in command to its default key assignment ' aKB.Rebind wdKeyCategoryCommand, "TestKeybinding" ' Rebinds a key to another command ' aKB.Execute ' Executes the key binding command Debug.Print "---" Next aKB Debug.Print "Ending ListAvailableKeyBindings" End Sub '--------------------------------------------- '7 7 7 7 |
#5
|
||||
|
||||
![]()
Have you tried the first option I suggested?
1. Save a copy of your template that has the keyboard bindings in it 2. Remove all the keyboard customisations from the template itself. 3. Load the copied template as an addin Now go ahead and edit the docs and verify that the keyboard shortcuts work as expected. If this is working, disable the addin and verify the default key actions now happen. If that is as expected, all you need is a macro to toggle the addin either on or off.
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#6
|
|||
|
|||
![]()
Thanks very much. I have been working on the code but I only briefly looked at your suggestions. Tomorrow, or Thursday, I will do more work and get back to you.
Are you interested in fee-for-service or contracting of a consultant for this project. If so, what is your going rate. We can discuss a budget. If this is not feasible, do you have someone in mind who would be interested? Thanks MS |
#7
|
|||
|
|||
![]()
30ai.dotm text is:
[1] text [2] text [] [3] text d [Monday] text add [1] asdfasd [asadfasdf] The code is Option Explicit Sub RevertEndKey() On Error Resume Next Application.KeyBindings.Clear keyCode:=BuildKeyCode(wdKeyEnd) On Error GoTo 0 End Sub Sub AssignEndKey() On Error Resume Next Application.KeyBindings.Add keyCode:=BuildKeyCode(wdKeyEnd), _ keyCategory:=wdKeyCategoryMacro, Command:="SelNext" On Error GoTo 0 End Sub Sub CreateRevertHotkey() On Error Resume Next Application.KeyBindings.Add keyCode:=BuildKeyCode(wdKeyShift, wdKeyControl, wdKeyF8), _ keyCategory:=wdKeyCategoryMacro, Command:="RevertEndKey" On Error GoTo 0 End Sub Sub CreateAssignHotkey() On Error Resume Next Application.KeyBindings.Add keyCode:=BuildKeyCode(wdKeyShift, wdKeyControl, wdKeyF9), _ keyCategory:=wdKeyCategoryMacro, Command:="AssignEndKey" On Error GoTo 0 End Sub Sub SelNext() On Error GoTo ErrHandler Dim findText As String findText = "\[*\]" With Selection.Find .Text = findText .Replacement.Text = "" .Forward = True .Wrap = wdFindStop .Format = False .MatchWildcards = True .Execute End With If Selection.Find.Found Then ' Do nothing, found and selected text Else ' Do nothing, no text found End If Exit Sub ErrHandler: ' Handle error silently End Sub cannot get the assigned key to revert, my coding is limited and chatgpt is useless any suggestions appreciated |
![]() |
Tags |
disable macro, enable macro, toggle macro assignment |
Thread Tools | |
Display Modes | |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
MikeyLasco77 | Word | 6 | 03-02-2023 07:08 AM |
Word Refuses to Allow Macros, even when All Macros Enabled | devlon | Word VBA | 3 | 10-04-2022 02:15 PM |
![]() |
gjone43 | Mail Merge | 6 | 10-15-2020 08:15 PM |
vba editor - how to toggle the continuous display of all macros in a module | floattube | Word VBA | 0 | 11-28-2014 01:35 PM |
Is there a way to toggle the contents that are displayed in a Word document? | sclind | Word | 1 | 02-24-2012 04:56 PM |