Solved

reset keybinding word vba macro

Posted on 2014-01-12
11
774 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
 
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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

MS Access 2003 or later To MySQL Migration Project Hello All, this is my second article in the category of MS-OFFICE Automation. In internet I am not able to find any comprehensive resource on the Migration of MS Access back-end to MySQL so I fin…
The new Microsoft OS looks great, is easier than ever to upgrade to, it is even free.  So what's the catch?  If you don't change the privacy settings, Microsoft will, in accordance with the (EULA) you clicked okay to without reading, collect all the…
Viewers will learn how to maximize accessibility options in an Excel workbook for users with accessibility issues.
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 …

911 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

15 Experts available now in Live!

Get 1:1 Help Now