Solved

reset keybinding word vba macro

Posted on 2014-01-12
11
871 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Many companies are making the switch from Microsoft to Google Apps (https://www.google.com/work/apps/business/). Use this article to learn more about what Google Apps has to offer and to help if you’re planning on migrating to Google Apps. It is …
The System Center Operations Manager 2012, known as SCOM, is a part of the Microsoft system center product that provides the user with infrastructure monitoring and application performance monitoring. SCOM monitors:   Windows or UNIX/LinuxNetwo…
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…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Suggested Courses

617 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