Let user click in doc while macro runs?

E

Ed M

I have a macro which requires the user to select where the action will
happen. I have two issues to deal with: (a) I'm making this a Private Sub,
dependent on the running of a previous macro, because I need to pass a total
count variable from the previous macro into this one. (b) This is a
repeated action, so this macro will update a counter every time it's run and
compare it with the passed-on variable to know when to stop.

The macro will find a generic label statement and replace it with a
user-chosen label (input earlier via InputBox) that includes a number (the
counter which increments each time the loop is run).

Is there any way I can break out of the loop to ask the user to "Place the
cursor where you want the text to be", then let the macro continue? If not,
what might be the best way to handle this?

Ed
 
C

Charles Kenyon

My klutzy way of handling this would be two macros and a custom toolbar. The
toolbar would normally be disabled. It would have a button to run the second
macro. The first macro would display a message box telling the user to click
where the text will be inserted, and enable the custom toolbar and make it
visible.

Consider using a document variable for your counter. It may require fewer
resources.

Note that I consider myself still a novice when it comes to vba programming.
The experts may have much more elegant solutions. They are probably relaxing
and watching parades or football (US) today. Happy New Year.
--

Charles Kenyon

Word New User FAQ & Web Directory:
<URL: http://addbalance.com/word/index.htm>

Intermediate User's Guide to Microsoft Word (supplemented version of
Microsoft's Legal Users' Guide)
<URL: http://addbalance.com/usersguide/index.htm>

See also the MVP FAQ: <URL: http://www.mvps.org/word/> which is awesome!
--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.
 
E

Ed M

Thanks, Charles. I hadn't thought of that "split personality" approach, but
it does make sense.

My only concern, then, is passing that "TotalCount" variable into the second
half of the procedure. I'm still *very* novice myself, so I'm not sure if
I've got the right idea. I'm thinking that, in the same module as my main
(the previous) macro, I have a Private Sub, which would be the second one,
with Private Sub Second(TotalCount) being the way to pass the variable on.
And I would call this inside the first macro, just before the End Sub. I
hope I have that right.

Now, if I do the "two-stage" process, would I have two Private Sub macros?
Would I have to call the "second stage" from within the first, just as I'm
calling the first from with the main? Would both need Private Sub (Total
Count) to make sure the variable got all the way down to where it was
needed?

Thanks for being here today. Happy New Year!

Ed (too late for parades, not interested in football)
 
C

Cindy M -WordMVP-

Hi Ed,

You might consider asking the user at the beginning, in a Message Box if the
cursor is in the correct location. If he answers "No", stop the macro and the
user can start it again after changing the selection. There are ways to work
with multiple macros, though.

Otherwise, I agree with Charles that you have to have two separate macros.
However, you can't use Private Subs for anything the user has to call; or, at
least, not directly. You could have one Public "entry" macro that checks
certain conditions, then calls the appropriate Private Sub. But only Public
Sub can be assigned to toolbar buttons or keyboard shortcuts.

Obviously, you need to be able to store variable data in such a way that it
will be available outside the execution of certain Subs.

One way to do this would be to write it to a document variable
(ActiveDocument.Variables("TheName").Value = "data").

Another method would be to save the data to an outside text file, or to a
Registry entry. This would be especially appropriate for data you'd need
between Word sessions.

Something that can work, but depends on VBA not going out of scope, is to
declare a public variable at the module level. Under normal circumstances,
this value will remain in memory until Word is exited (or the project
containing it is closed).
I have a macro which requires the user to select where the action will
happen. I have two issues to deal with: (a) I'm making this a Private Sub,
dependent on the running of a previous macro, because I need to pass a total
count variable from the previous macro into this one. (b) This is a
repeated action, so this macro will update a counter every time it's run and
compare it with the passed-on variable to know when to stop.

The macro will find a generic label statement and replace it with a
user-chosen label (input earlier via InputBox) that includes a number (the
counter which increments each time the loop is run).

Is there any way I can break out of the loop to ask the user to "Place the
cursor where you want the text to be", then let the macro continue? If not,
what might be the best way to handle this?

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Sep 30 2003)
http://www.mvps.org/word

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
E

Ed

Thanks for your advice, Cindy. I'm thinking this will need to be done
actually in three steps: the main macro that sets everything up, then the
two-stage routine that requires user input. If I understand it correctly, I
should probably write all three in one module, with a Public variable
declaration at the top of the module, before any Sub routines start. Does
this sound right?

Hope your holidays were good!

Ed
 
C

Cindy M -WordMVP-

Hi Ed,
I'm thinking this will need to be done
actually in three steps: the main macro that sets everything up, then the
two-stage routine that requires user input. If I understand it correctly, I
should probably write all three in one module, with a Public variable
declaration at the top of the module, before any Sub routines start. Does
this sound right?
From the little I know about what it is you actually need to do, it sounds
like this would do what you need, yes.

Cindy Meister
 
E

Ed

From the little I know about what it is you actually need to do, it sounds
like this would do what you need, yes.

Oh - I guess a little bit of information in that direction *might* help!
8>\

The reports I write have lots of tables. During the review process, it's
not unusual to be told I need to add, subtract, or shuffle these tables.
Each table needs its own header/caption, and each table needs to be
referenced in the preceding paragraph. I'm just tired of manually going
through a 60-to-70 page report and manually re-numbering each table! And so
are the other report writers.

The first routine would search through a report, identify tables that need a
caption (meeting certain criteria), and insert the caption. From this
macro, I get a count of how many captions were inserted.

Next, I need to select a specific caption and insert a cross-reference. The
idea is that with captions and cross-references, whether tables are
inserted, deleted, or moved, the numbering takes care of itself.

But this is where I need user input. So the second routine would identify
the caption to be referenced, and - using Charles' idea - pop up two message
boxes. The first asks, "Is this the correct caption to cross-reference?"
If yes, then another box that says, "To continue, close this box, click
where you want to insert the reference, and click the button on the custom
toolbar that will appear when this box closes." Clicking the button would
then insert the proper cross-reference and select the next caption, up to
the amount in the TotalCaptions variable passed in from the first routine.

This way, instead of going through the document at least twice each time
things change, I need to interact only at each question point, and then
again only with the changes, rather than with each table each time the
document is changed.

Does this sound workable? Or is my inexperience missing something?

Ed
 

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