how to Pause a VBA procedure in Word until user presses Enter

P

Paul James

I would like to be able to suspend execution of a VBA procedure in MS Word
so that the user can type some text into a document before the procedure
continues execution, and I would like VBA to resume the procedure when the
user presses the Enter key.

I recently posted this question in another VBA newsgroup, and someone did
offer a solution of bringing up a user form in modeless mode which would
enable the user to enter text in the user form while the procedure was
suspended, enter more text in a text box in the user form, then resume the
original procedure when the (modeless) user form was closed. However, I
would like to offer the user more flexibility during the suspension of the
Sub procedure than simply giving them the chance to enter some text in a
form.

It would be great for this purpose if VBA had some kind of a Pause function
that would temporarily suspend the execution of a Sub procedure until the
user pressed the Enter key or some other key. Can anyone tell me if there
is such a function, or if there's any way I can accomplish this?

Thanks in advance,

Paul
 
J

Jay Freedman

Hi, Paul,

No, VBA does not have any kind of Pause function. Other than the nonmodal
userform, your only option is to break the macro into two separate macros.
The "pause" occurs when the first macro ends. You then need to provide some
way for the user to start the second macro when they complete whatever they
want to do -- with a MacroButton field, a toolbar button, a keyboard
shortcut, or the exit macro of a particular form field.
 
K

Klaus Linke

Jay Freedman said:
[...] You then need to provide some way for the user to
start the second macro when they complete whatever they
want to do -- with a MacroButton field, a toolbar button, a
keyboard shortcut, or the exit macro of a particular form field.


Hi Paul,

In principle, you could have the first macro set up the "Enter" key as a
keyboard shortcut to the second macro.
The second macro could then remove this keyboard shortcut again.

But I don't know if I would risk that. For one, keyboard shortcuts linked
to regular keys are a bit dangerous if you're not sure they will get
deleted again (... what if the user quits Word before the second macro
runs?), and also, intercepting the "Enter" key might confuse Word (perhaps
the spell checker won't check the word you entered before using "Enter"?
....or more dramatic things might go wrong).

I'd rethink the logic of your macro. Say if the user is expected to type in
some information that then gets properly formatted or cross-referenced,
perhaps a user form might be more appropriate where the user types in the
text in a text box, and this text then gets inserted into the doc at the
proper location.
Or a text form field (from the "Forms" toolbar) might allow you to validate
or format the entered text when the user leaves the field.

The problem with your approach is, you can't really be sure what the user
might do before pressing "Enter" the next time.
S/He might scroll to a different part of the document, cut/paste stuff,
switch documents, whatever.

Regards,
Klaus
 
P

Paul James

Thanks for your suggestions, gentlemen. Based on your comments, I think I
know what to do.

What I'm trying to accomplish is this - the procedure inserts some
boilerplate text while the user is creating a document. In the middle of
the boilerplate, however, the user needs to type in some text from the
keyboard. After that text is entered by the user, then I need to have VBA
insert the rest of the boilerplate.

After reading your suggestions, I'm thinking that the modeless user form is
probably the best way for me to go. I could have the procedure insert the
text, then display the user form, into which the user could type their
particular text, and when the user closes the form, the text from the text
box is entered into the document, followed by the remainder of the original
boilerplate. Where this will get more complicated is where I need to have
the user do this more than once within some boilerplate text. However, I
suppose I could accomplish this with more than one command button on the
user form.

Klaus, your discussion of redefining the Enter key was suggested to me by
someone who also gave me the code to do it. One sub redefined the Enter key
so that when it was pressed, it ran another sub which restored its default
definition. However, I was afraid of doing that for reasons similar to the
ones you expressed. What if processing were interrupted before the default
definition was restored? How would the user regain their control of the
Enter key? So I didn't even mention it.

I've got a final question - I'm new to VBA in Word; how do I refer to the
contents of a user form text box so VBA will insert that string into the
cursor location in the document when the user form closes? In Access or
Excel I'd use an expression like Forms!myForm!txtUserText.Value. Can I use
the same expression to refer to the form's text box in Word VBA?

Thanks again.
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < Paul James > écrivait :
In this message, < Paul James > wrote:

|| Thanks for your suggestions, gentlemen. Based on your comments, I think
I
|| know what to do.
||
|| What I'm trying to accomplish is this - the procedure inserts some
|| boilerplate text while the user is creating a document. In the middle of
|| the boilerplate, however, the user needs to type in some text from the
|| keyboard. After that text is entered by the user, then I need to have
VBA
|| insert the rest of the boilerplate.
||
|| After reading your suggestions, I'm thinking that the modeless user form
is
|| probably the best way for me to go. I could have the procedure insert
the
|| text, then display the user form, into which the user could type their
|| particular text, and when the user closes the form, the text from the
text
|| box is entered into the document, followed by the remainder of the
original
|| boilerplate. Where this will get more complicated is where I need to
have
|| the user do this more than once within some boilerplate text. However, I
|| suppose I could accomplish this with more than one command button on the
|| user form.
||
|| Klaus, your discussion of redefining the Enter key was suggested to me by
|| someone who also gave me the code to do it. One sub redefined the Enter
key
|| so that when it was pressed, it ran another sub which restored its
default
|| definition. However, I was afraid of doing that for reasons similar to
the
|| ones you expressed. What if processing were interrupted before the
default
|| definition was restored? How would the user regain their control of the
|| Enter key? So I didn't even mention it.
||
|| I've got a final question - I'm new to VBA in Word; how do I refer to the
|| contents of a user form text box so VBA will insert that string into the
|| cursor location in the document when the user form closes? In Access or
|| Excel I'd use an expression like Forms!myForm!txtUserText.Value. Can I
use
|| the same expression to refer to the form's text box in Word VBA?
||

Have you considered the following:

Create an Autotext from your complete Boiler plate text.
The boiler plate text should include bookmarks to represent the various
locations where the user text needs to be inserted.
Show a modal Userform asking for the user to type in the various strings of
customized text.
Insert the said Autotext.
Insert the textbox strings at all the appropriate locations using the
bookmarks.

This is very simple to design/implement and almost error proof.

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
E

Eliot

On 20040614 1:21 PM, in article #[email protected],
Linke20040614 1:21
(e-mail address removed)#[email protected]
Jay Freedman said:
[...] You then need to provide some way for the user to
start the second macro when they complete whatever they
want to do -- with a MacroButton field, a toolbar button, a
keyboard shortcut, or the exit macro of a particular form field.


Hi Paul,

In principle, you could have the first macro set up the "Enter" key as a
keyboard shortcut to the second macro.
The second macro could then remove this keyboard shortcut again.

But I don't know if I would risk that. For one, keyboard shortcuts linked
to regular keys are a bit dangerous if you're not sure they will get
deleted again (... what if the user quits Word before the second macro
runs?), and also, intercepting the "Enter" key might confuse Word (perhaps
the spell checker won't check the word you entered before using "Enter"?
...or more dramatic things might go wrong).

I'd rethink the logic of your macro. Say if the user is expected to type in
some information that then gets properly formatted or cross-referenced,
perhaps a user form might be more appropriate where the user types in the
text in a text box, and this text then gets inserted into the doc at the
proper location.
Or a text form field (from the "Forms" toolbar) might allow you to validate
or format the entered text when the user leaves the field.

The problem with your approach is, you can't really be sure what the user
might do before pressing "Enter" the next time.
S/He might scroll to a different part of the document, cut/paste stuff,
switch documents, whatever.

Regards,
Klaus
 
J

Jay Freedman

Jean-Guy Marcil said:
Bonjour,

Dans son message, < Paul James > écrivait :


Have you considered the following:

Create an Autotext from your complete Boiler plate text.
The boiler plate text should include bookmarks to represent the
various locations where the user text needs to be inserted.
Show a modal Userform asking for the user to type in the various
strings of customized text.
Insert the said Autotext.
Insert the textbox strings at all the appropriate locations using the
bookmarks.

This is very simple to design/implement and almost error proof.

I agree with Jean-Guy -- this is exactly the way I would design this kind of
application. An alternative is to design your template with the boilerplate
and bookmarks already in place, and call the userform from a macro named
AutoNew() which will automatically execute when the user selects the
template in the File > New dialog.

The underlying lesson is this: When designing a macro, don't always think in
terms of how you would do the same task manually. There are often faster,
better ways of achieving the same results in VBA with completely different
procedures and thought processes.

To answer your other question, you can use a similar syntax to refer to the
userform text boxes, but I would modify it a bit. In a userform's VBA code
(in any Office app, not just Word) you can use the keyword Me to refer to
the current instance of the userform, so all you need for the text is
Me.txtUserText.Value. (In fact, you can leave off the "Me." part and VBA
will assume it, but that can be dangerous if there may be more than one
userform.)
 
P

Paul James

Bon jour, mes amis,

I'm not familiar with the use of Autotext or Bookmarks, so I'll have to look
those up. From what you've both said, it sounds like you can do some useful
things with those features.

Thanks so much for these suggestions.

Paul
 

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