Solved

trigger Word macro by pressing space bar (only).  Don't want to use ctrl, F key, etc.

Posted on 2014-01-09
7
670 Views
Last Modified: 2014-01-14
I have a search and replace macro that I recorded in Word.  To integrate it into the work flow, I'd like to trigger it by a press on the space bar without having to use the ctrl key, etc.  That way, if there is a replacement to be done, it will happen as I type rather than having to interrupt my typing to press a special key combination.

I know about autotext.  I don't like it.  My macro is also for inserting blocks of text as well as individual words.  It's easier to organize what I am doing with the VBA code rather than mix up my text replacement with all the autotext prefab stuff.

This feature would be used on my work computer which is locked down by IT and doesn't allow me to install .exe files.  But I can work within the Office VBA environment.  Or maybe Office applications can use another scripting or programming technique.
0
Comment
Question by:rreiss60
  • 5
7 Comments
 
LVL 31

Expert Comment

by:Paul Sauvé
ID: 39768628
If you use only the "space bar" as a macro shortcut, how do you intend to insert a space in your document? Every time you press the space bar, the macro will execute!
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 39768709
Shortcut keystrokes have to begin with Alt or Ctrl, so you could run a macro from some such combination. Not a space, though.

I have looked at the extra events that can be used by creating a WithEvents application object, but the closest is SelectionChange, which is not triggered when typing in characters.
0
 

Author Comment

by:rreiss60
ID: 39769094
I figure that if there is no text to replace, the macro may execute but there is no visible effect since the find part of the macro doesn't find anything.  I just go on typing.
 
I found this code, but instead of Alt + 0, what would I put in for spacebar?

' \\ Code for Module1
Option Explicit
 
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



 ' \\ Code for Module2
Option Explicit
 
 ' \\ Test sub for keybinding
Sub TestKeybinding()
    MsgBox "We have a winner", vbInformation, "Succes"
End Sub

My find and replace function would go in module 2.
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 

Author Comment

by:rreiss60
ID: 39769106
wdKeySpacebar is the answer.

Now I will put in my find and replace in module 2 instead of the msgbox.
0
 

Author Comment

by:rreiss60
ID: 39769982
Oops.  Need to disable keybinding after text replacement to restore normal spacebar function.  Any help appreciated.
0
 

Accepted Solution

by:
rreiss60 earned 0 total points
ID: 39770017
With FindKey(BuildKeyCode(wdKeySpacebar)
 MsgBox .Command & " is assigned to spacebar"
 .Disable

End With

This code doesn't restore spacebar to normal behavior.  Not sure what else to try.  Would code go in module 1 or 2?
0
 

Author Closing Comment

by:rreiss60
ID: 39778800
expert comments didn't lead to successful resolution. I was able to come up with a portion of the answer I needed. Would have liked someone to follow up on what I started.
0

Featured Post

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.

Question has a verified solution.

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

Suggested Solutions

A few years ago I was very much a beginner at VBA, and that very much remains the case today.  I'll do my best to explain things as I go in the hope that other beginners can follow.  If you just want to check out a tool that creates a Select Case fu…
This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.
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…

919 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

16 Experts available now in Live!

Get 1:1 Help Now