Solved

reset keybinding word vba macro

Posted on 2014-01-12
11
762 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 26

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
 
LVL 26

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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

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 26

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This very simple solution applies to a narrow cross-section of the "needs to close" variety. In this case, the full message in Event Viewer was in applog, Event ID 1000: Faulting application iexplore.exe, version 8.0.6001.18702, faulting module …
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
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…

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now