Solved

reset keybinding word vba macro

Posted on 2014-01-12
11
789 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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

We were having a lot of "Heartbeat Alerts" in our SCOM environment, now "Heartbeat" in a SCOM environment for those of you who might not be familiar with SCOM is a packet of data sent from the agent to the management server on a regular basis, basic…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Viewers will learn the different options available in the Backstage view in Excel 2013.
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…

776 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