vba - Trigger macro at text+F9 -


i run macro if text has been typed , if f9 pressed afterwards, example:

the patient has been diagnosed dia1  

, followed f9 key.

word should react on f9 key , run macro dia1 erase word "dia1" typed text , insert text.

how this?

two things:

1- solution finds "dia1" within last line, doesn't check last word. if insist on last word here is:

sub lastword()     dim rng range, wrd string     set rng = activedocument.paragraphs.last.range     wrd = strreverse(trim(left(strreverse(rng), instr(1, strreverse(rng), " ", vbtextcompare))))     msgbox wrd end sub 

2- f9-f12 reserved closest option f8. alternatively can assign macro f9 following these steps, @ibo suggested.

solution:

1-bind f8 macro

sub bind()     application.customizationcontext = thisdocument.attachedtemplate     keybindings.add keycode:=buildkeycode(wdkeyf8), keycategory:=wdkeycategorymacro, command:="runme" end sub 

2- "find , replace" macro:

sub runme()     dim rng  range     set rng = activedocument.paragraphs.last.range     rng.find.execute findtext:="dia1", replacewith:="hello", replace:=wdreplaceall end sub 

3-if need unbind f8:

sub unbind()     customizationcontext = normaltemplate     findkey(buildkeycode(wdkeyf8)).clear end sub 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -