Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-25-2024, 03:19 PM
savvy savvy is offline toggle macros assigned to key in Word 365 Windows 10 toggle macros assigned to key in Word 365 Office 2019
Novice
toggle macros assigned to key in Word 365
 
Join Date: Aug 2020
Location: Toronto
Posts: 23
savvy is on a distinguished road
Default toggle macros assigned to key in Word 365

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
Reply With Quote
  #2  
Old 06-25-2024, 05:27 PM
Guessed's Avatar
Guessed Guessed is offline toggle macros assigned to key in Word 365 Windows 10 toggle macros assigned to key in Word 365 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
  #3  
Old 06-29-2024, 09:12 AM
savvy savvy is offline toggle macros assigned to key in Word 365 Windows 11 toggle macros assigned to key in Word 365 Office 2019
Novice
toggle macros assigned to key in Word 365
 
Join Date: Aug 2020
Location: Toronto
Posts: 23
savvy is on a distinguished road
Default

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
Reply With Quote
  #4  
Old 06-29-2024, 09:18 AM
savvy savvy is offline toggle macros assigned to key in Word 365 Windows 11 toggle macros assigned to key in Word 365 Office 2019
Novice
toggle macros assigned to key in Word 365
 
Join Date: Aug 2020
Location: Toronto
Posts: 23
savvy is on a distinguished road
Default

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
Reply With Quote
  #5  
Old 06-30-2024, 09:28 PM
Guessed's Avatar
Guessed Guessed is offline toggle macros assigned to key in Word 365 Windows 10 toggle macros assigned to key in Word 365 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

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
Reply With Quote
  #6  
Old 07-03-2024, 12:16 AM
savvy savvy is offline toggle macros assigned to key in Word 365 Windows 11 toggle macros assigned to key in Word 365 Office 2019
Novice
toggle macros assigned to key in Word 365
 
Join Date: Aug 2020
Location: Toronto
Posts: 23
savvy is on a distinguished road
Default

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
Reply With Quote
  #7  
Old 07-05-2024, 05:17 AM
savvy savvy is offline toggle macros assigned to key in Word 365 Windows 11 toggle macros assigned to key in Word 365 Office 2019
Novice
toggle macros assigned to key in Word 365
 
Join Date: Aug 2020
Location: Toronto
Posts: 23
savvy is on a distinguished road
Default

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
Reply With Quote
Reply

Tags
disable macro, enable macro, toggle macro assignment



Similar Threads
Thread Thread Starter Forum Replies Last Post
toggle macros assigned to key in Word 365 Autosave toggle missing completely in Word 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
toggle macros assigned to key in Word 365 MERGESEQ does not appear to be having a value assigned 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

Other Forums: Access Forums

All times are GMT -7. The time now is 08:00 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft