Avatar of rreiss60
rreiss60Flag for United States of America

asked on 

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

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.
Microsoft ApplicationsMicrosoft Word

Avatar of undefined
Last Comment
rreiss60
Avatar of Paul Sauvé
Paul Sauvé
Flag of Canada image

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!
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

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.
Avatar of rreiss60
rreiss60
Flag of United States of America image

ASKER

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.
Avatar of rreiss60
rreiss60
Flag of United States of America image

ASKER

wdKeySpacebar is the answer.

Now I will put in my find and replace in module 2 instead of the msgbox.
Avatar of rreiss60
rreiss60
Flag of United States of America image

ASKER

Oops.  Need to disable keybinding after text replacement to restore normal spacebar function.  Any help appreciated.
ASKER CERTIFIED SOLUTION
Avatar of rreiss60
rreiss60
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of rreiss60
rreiss60
Flag of United States of America image

ASKER

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.
Microsoft Applications
Microsoft Applications

Microsoft applications include a variety of software programs, including development and digital authoring programs (Expression and Media Center), educational programs, Internet software, including Essentials, Skype and the Live family, anti-virus, productivity applications and suites like Office, Excel, Word, Outlook, Access and PowerPoint, video games and server applications such as Exchange, SharePoint, IIS and Virtual Server.

45K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo