Invoke a macro using the last-typed word as a macro name?

  • Thread starter Kathy Collingswood
  • Start date
K

Kathy Collingswood

I've previously asked about using an AutoCorrect entry to open a
UserForm or invoke some code. I have a feeling that this may not be
possible, but have an idea that would simulate it.

Could someone please sketch out the code for:

As I'm typing a document, instead of typing the next word in the
sentence, I type the name of a macro.

After the last character of the macro name I just typed, I press a
function key, or an Alt+SomeLetter combination.

At that time, Word deletes the previously-typed macro name from the
document and invokes a macro having the same name as the
previously-typed-but-now-erased word. (This would either insert text
at the cursor, or open a userform. The fields in the userform would
then be put into a string that would insert some canned text at the
cursor.)

If you can make any sense out of what I just wrote, please head me in
the right direction ...

TIA
 
S

Steve Lang

Hi Kathy,

It is very possible to do what you are asking (id it all the time!) Here is
an example.

Depending on what characters the user enters before pressing your keyboard
trigger, either a call to display a userform (AmendNACChapter), a sub in a
referenced template, or a regular Autotext entry is run. You'll need to map
the keystroke you want the user to use to call this in the template where
you want to use it.

--------Code to Bind sub to a keystroke (only need to run once against your
template--------------
CustomizationContext = 'path to your template
KeyBindings.Add KeyCategory:=wdKeyCategoryMacro,
KeyCode:=(BuildKeyCode(wdKeyF3)), Command:="doAutoText"
----------------------------------------------------------------------------
--------------------------
------------Hijacked Autotext entry code------------------
Sub doAutotext()
Application.ScreenUpdating = False
Selection.MoveLeft unit:=wdWord, Count:=1, Extend:=wdExtend
On Error GoTo trap
Select Case Selection.Text
Case "ch"
Call AmendNACChapter
Case "s", "S"
With Selection
.Delete unit:=wdCharacter, Count:=-1
.Style = "Normal"
.Style = "Default Paragraph Font"
.Font.Size = 12
Autotext = Drafting.DraftingGlobal.sF3
End With
Case Else
Selection.Range.InsertAutoText
End Select
Selection.Collapse (wdCollapseEnd)


Exit Sub

trap:
MsgBox "Make sure the word immediately preceding the cursor is a
known autotext entry.", vbCritical, "Autotext Error..."

End Sub

------------------------------

HTH and have a great day!

If you have additional questions that the rest of the group may not be
interested in, contact me directly.

--
Steve Lang
Legislative Counsel Bureau
Carson City, NV
GMT+8
slang at lcb hereisadot state anotherdot nv onemoredot us
 
M

Mouse

Why not just write your macro and assign it a hot key
(alt + some key(s)). Then, when you reach the point at
which you wish to insert your canned text hit the hot key
(s) and have the macro insert the text. I use this to
insert forms and other minor but consistent stuff all the
time.
 
K

Kathy Collingswood

Thank you Steve!

Wow!
Made my day.

Clearly, great minds run on the same track <grin>...

Good thing for me yours was there first. I can only imagine the
kludge of trial and error and debugging I would have been pulling
behind me ...

You're the best.

Kathy
 
L

Larry

Kathy,

May I ask the purpose of this? If the goal is to run a macro, why not
just assign a keystroke to that macro instead of typing the whole name
of the macro?

Larry
 
J

JGM

Hi Kathy,

I've got to ask...
What is the purpose/context for your procedure? I've been trying very hard
to come up with a scenario where your query would be the ideal solution...
and I keep hittting the same wall:
Why not either use a customized hotkey to call the macro, a button on a
toolbar or do ALT-F8 and choose the macro from the list? I am assuming it is
for personal use, right? Or is it a sytem you intend to distribute to
various users? If it is the latter, then what is the advantage of doing it
this way for those users?

Wouldn't any of those three be less likely to cause errors than typing a
word that would activate a complicated code to select the said word (linked
to an autotext entrry) and then execute the macro called by the same
word....? I want to see the light!

Basically, I would like to know why you need to do this so that if someday I
come up with the same situation... then I will have you to thank for, for
having shown me the way!

Thanks in advance.
 
S

Steve Lang

Hi Jean-Guy - and Larry,

Since I provided to Kathy a way to do it, I can tell you why and how I use
this method.

Originally we had a number of Autotext entries (converted from a VAX-based
processor to Word95) that my users used to enter formatted text into their
documents. As time passed, they wanted more and more flexibility in the way
the text was created and entered. Instead of generating hundreds of
potential autotext entries or an equally large number of complicated
keyboard shortcuts and macros to choose from, they simply enter the same
text trigger they always did, and a form (or other mechanism) would appear
to allow them to change or customize the "standard" autotext entry. This
way, my users didn't have to change how they worked, but were given
tremendous additional flexibility in generating their work product.
I could have used buttons, or select from a macro list, but that wasn't an
effective solution. I already have numerous custom menus and somewhere in
the neighborhood of 400 individual subs and functions. It would be difficult
for my users to wade through all that to get to the one they wanted -
assuming they knew.

HTH and have a great day!

Steve
 
J

JGM

Thanks Steve,

And the light was! That makes sense... customizing Autotext entries on a
case by case basis and on the fly is in an interesting concept... I'll have
to remember that!

Have a good one!
 
C

Cathy Collingswood

Sorry it took so long to respond ...

I'm trying to reproduce a WordPerfect 5.0 DOS environment that has
been in use for about a hundred years. The users are relentlessly
typing mundane medical reports that have many phrases inserted in
them. A lot of those phrases are already assigned to keystroke
macros, and they will be simply reproduced, and everybody will be
happy with that.

The users don't want to remove their hands from the keyboard to use a
mouse AT ALL, because it breaks the long-standing tradition and costs
them keyboard time and frowns from the supervisors. Not only that,
but the use of the mouse itself (which is viewed as a mysterious
article of the highest technology) to do anything NEW is grounds for a
nervous breakdown.

BUT ... a whole new set of macros has been recently introduced, and
there are not enough potential keystroke macros around the keyboard to
accommodate them all. I suggested the idea of assigning 3-character,
easy-to-remember acronyms to them, and described to the management how
that would work ... that the typist would simply type say, "adb" at
the insertion point, then press Alt+A for example, and the text
"attention deficit high blood pressure disorder" would appear without
the typist taking his/her hands from the keyboard. It was also
decided that the acronym association would be easier to remember than
the hotkey sequence Ctrl+Alt+Shift+F1 for example, which takes more
fingers than most people have ...

It would also be easier to train new people in the acronyms ... and
that's important because the turnover rate in theTyping Factory is,
shall we say, "considerable".
 
J

JGM

Hi Cathy,

I still don't get it!
Why do you need macros at all if your purpose is to provide the user with
shortcuts to insert text?
Autotext entries are just the ticket...no?

"adb" + Enter or F3 and you get whatever you want... i.e. "attention deficit
high blood pressure disorder" or any other text, table, paragraphs you care
to assign to that shortcut. You can give your autotext entries any name you
want, but as soon as you type the first 3 or 4 characters, a suggestion pops
up and you hit Enter or F3 (F3 will work even if the suggestion does not pop
up...).
Just store the autotext in a global template and everyone has acces to
them...

Finally, it is very easy to train people to manage autotext...

Wouldn't that do the trick?
Just curious!

Cheers!
 
K

Kathy Collingswood

Sorry Jean-Guy, I left out a small, but essential piece of this, but
thank you for your continuing interest:

A number of the macros from the WP 5.0 DOS past make use of something
that's like a Basic "Input" statement, where the user, after invoking
a keystroke macro, "fills in" a series of these, like "Person's Name,"
"Ward," "Hospital ," "Date of Incarceration" etc. which are presented
sequentially as the user enters data and presses "Enter" to move on.
After the last one is filled in, the macro inserts a string like "Mary
Smith, was admitted to the maternity ward in Portland Hospital on
3/12/03."

In order to emulate this, I decided to use a userform (my first),
wherein the mnemonic- (a word I should have used yesterday instead of
"acronym," but couldn't remember ... it came to me in a dream last
night ... which is probably a message from God telling me to change my
career.) generated macro launches the userform, which contains the
necessary fields, which can be filled in using the Tab key to advance
from one to the next. The last tabbed control is an OK key, which,
upon "Enter" inserts the text in the document. (Again, no need to
ever leave the keyboard to use the dreaded mouse.)

Some time ago, I posted a question on
microsoft.public.word.vba.userforms, asking the question whether an
autotext entry could be used to invoke a macro, and didn't get a
positive reply. If *that* could be done, I'd be very, very happy.

Any ideas on that ... or any other way to do what I'm attempting?

Regards,

Kathy
 
J

JGM

Hi Kathy,

Ah! Now I get it!
Some time ago, I posted a question on
microsoft.public.word.vba.userforms, asking the question whether an
autotext entry could be used to invoke a macro, and didn't get a
positive reply. If *that* could be done, I'd be very, very happy.

What do you mean? You did get a reply from Steve Lang... I remember even
discussing that reply with him...

In case you cannot find it anymore, here it is:

Quote

Hi Kathy,

It is very possible to do what you are asking (id it all the time!) Here is
an example.

Depending on what characters the user enters before pressing your keyboard
trigger, either a call to display a userform (AmendNACChapter), a sub in a
referenced template, or a regular Autotext entry is run. You'll need to map
the keystroke you want the user to use to call this in the template where
you want to use it.

--------Code to Bind sub to a keystroke (only need to run once against your
template--------------
CustomizationContext = 'path to your template
KeyBindings.Add KeyCategory:=wdKeyCategoryMacro,
KeyCode:=(BuildKeyCode(wdKeyF3)), Command:="doAutoText"
----------------------------------------------------------------------------
--------------------------
------------Hijacked Autotext entry code------------------
Sub doAutotext()
Application.ScreenUpdating = False
Selection.MoveLeft unit:=wdWord, Count:=1, Extend:=wdExtend
On Error GoTo trap
Select Case Selection.Text
Case "ch"
Call AmendNACChapter
Case "s", "S"
With Selection
.Delete unit:=wdCharacter, Count:=-1
.Style = "Normal"
.Style = "Default Paragraph Font"
.Font.Size = 12
Autotext = Drafting.DraftingGlobal.sF3
End With
Case Else
Selection.Range.InsertAutoText
End Select
Selection.Collapse (wdCollapseEnd)


Exit Sub

trap:
MsgBox "Make sure the word immediately preceding the cursor is a
known autotext entry.", vbCritical, "Autotext Error..."

End Sub

------------------------------

HTH and have a great day!

If you have additional questions that the rest of the group may not be
interested in, contact me directly.

--
Steve Lang
Legislative Counsel Bureau
Carson City, NV
GMT+8
slang at lcb hereisadot state anotherdot nv onemoredot us

End Quote

Or, try
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=eqKR6XvoDHA
..2488%40TK2MSFTNGP12.phx.gbl&rnum=2

for the complete thread (I gathered that most of this thread has been
archived...)
(Watch out for the possible hyperlink on 2 lines...)

Cheers!
 
K

Kathy Collingswood

Jedan-Guy,

My God! I somehow missed that message ... the only one showing under
mine is one from Word Heretic, which wasn't at all heretical, but
didn't really answer my question.

THANK YOU for posting it here, and Steve Lang for answering my
question.

I'm using Forte Agent ... and this is the first time I have lost a
message ... then again, I wouldn't know about the other ones ... would
I?

Good Grief. I just noticed another posted there on 11/10 with 7
replies, one of them from you intended for Doug Robbins, that suddenly
appeared this morning. I'll have to apologize about that one ...

I *really* appreciate this ...

Now ... off to give it a try ...

Regards,

Kathy
 
J

JGM

Hi Kathy,

Don't sweat it... With all the havoc caused by the Swen virus and the other
worm (Can't remember its name), news servers and ISP's are going crazy, lots
of strange things are going on! I had to do a Google with your name to find
Steve's message (I could not remember when or where we had had that
discussion...)

You don't have to give me thousands apologies either! With all the help and
knowledge I get from these groups, I do what I can to share it back.

Have a great weekend!

Cheers!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top