Solved

reset keybinding word vba macro

Posted on 2014-01-12
11
812 Views
Last Modified: 2014-01-12
I found this code on the internet, VBA Express website, I believe:

'Module1:

Sub AddKeyBinding()
    With Application
         ' \\ Do customization in THIS document
        .CustomizationContext = ThisDocument
         
         ' \\ Add keybinding to this document Shorcut: Alt+0
        .KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyAlt, wdKey0), _
        KeyCategory:=wdKeyCategoryCommand, _
        Command:="TestKeybinding"
    End With
End Sub

"TestKeybinding" is a sub in another module, "module2"

If I wanted to reset the keybindings back to the default setting, how should the code be modified?  Would I put it at the end of module2, or at the end of module1?
0
Comment
Question by:rreiss60
  • 8
  • 3
11 Comments
 

Author Comment

by:rreiss60
ID: 39775218
I want to do something like this after executing the second macro.

KeyBindings.ClearAll
    DoEvents
0
 
LVL 27

Expert Comment

by:MacroShadow
ID: 39775219
To reset all keybindings back to their default setting, use this macro:

Sub RemoveCustomFunctionKeyBindings() 
   CustomizationContext = NormalTemplate
   KeyBindings.ClearAll
End Sub

Open in new window


The macro can be placed in any normal module.
0
 

Author Comment

by:rreiss60
ID: 39775221
Thank you.  But, I'd like to call this from another macro after that first macro sub executes.  I don't want to go to the macros menus and select this.  Is there a way that one module can issue a command to execute the one you just provided?
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 27

Expert Comment

by:MacroShadow
ID: 39775228
Sure!

Sub AddKeyBinding()
    With Application
         ' \\ Do customization in THIS document
        .CustomizationContext = ThisDocument
         
         ' \\ Add keybinding to this document Shorcut: Alt+0
        .KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyAlt, wdKey0), _
        KeyCategory:=wdKeyCategoryCommand, _
        Command:="TestKeybinding"
    End With

   CustomizationContext = NormalTemplate
   KeyBindings.ClearAll

End Sub

Open in new window

But that kind a defeats the purpose, the custom keybindings will never be available.
0
 

Author Comment

by:rreiss60
ID: 39775234
I find that if I just at Keybindings.clearall to the last line of a macro that did the custom key bindings in the first place, the clearall function doesn't work.  It's only when I put "clear all" in a separate macro and run it that a reset of the bindings actually occurs.

What is the preferred way to handle this issue, or am I doing something wrong?
0
 

Author Comment

by:rreiss60
ID: 39775235
I want to toggle back and forth.
0
 

Author Comment

by:rreiss60
ID: 39775248
Thanks, Pasted this code but if I hit Alt + 0, the custom code still executes.  I want the command "TestKeybinding" to execute (it shows a msgbox). Then have Alt +0 stop working.

Things are still bound in a custom fashion.  But, if you put the "clearall" in a separate macro and run that separately, the default binding is restored.  So,  how does one call the separate macro programmatically without having to go to the macros menu?
0
 

Author Comment

by:rreiss60
ID: 39775264
Something like:

Application.Run MacroName:=”MacroNameFromList” ?
0
 
LVL 27

Accepted Solution

by:
MacroShadow earned 300 total points
ID: 39775293
Just call it by its name i.e.:

Sub Demo()
     ' Your code
     ' Your code

     ' call the restore default keybondings
     RemoveCustomFunctionKeyBindings
End Sub
0
 

Author Closing Comment

by:rreiss60
ID: 39775325
Still doesn't reset the bindings to default.  The code is executing since I have a msgbox confirm execution of the clearbindings macro. Awarding points for your greatly appreciated efforts. There must be a way to have a custom key binding execute in a macro and then return that keybinding to it's default status.
0
 

Author Comment

by:rreiss60
ID: 39775346
I added DoEvents after the keybindings.clearall.  This helps but if that macro is called by another one, it doesn't work.

So, Module1 sets the custom bindings and calls module 2.  Module 2 has keybindings.clearall statement.  Without DoEvents, it doesn't work.  With DoEvents it does, IF you execute module 2 directly from the macros menus.  But things aren't reset when module 1 calls module 2.
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: Leon
Software Metering within our group of companies has always been an afterthought until auditing of software and licensing became a pain point. Orchestrator and SCCM metering gave us the answer and it was an exciting process.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question