Repeating Action

S

Scott

I want to create a macro that does a set of instructions
one time. Now I may want to run that macro 4 times or
maybe 100 times in the same document and I dont want to
have to click a button 100 times to get my final result.
I could add a For/Next loop that would run the macro 100
times but I dont want to add For/Next loops to every new
macro. What I would like is to creat something that
repeats the next action a specified number of times. I
would click on a button that asks how many times I want
to do the next action, then I would run the macro and it
would do it how ever many times I specified.

Any thoughts?
 
D

Doug Robbins - Word MVP

Hi Scott,

Use an InputBox to ask the question and use the result in a For To
construction.

Please respond to the newsgroups for the benefit of others who may be
interested.

Hope this helps
Doug Robbins - Word MVP
 
S

Scott

Doug,

I considered an input box but I would have to insert that
code into each new macro. Is there any way to set it up
so that in can be used with different macros? I want to
click a button for repeating, enter the number of times I
want it to repeat, then click on the action I want it to
do and it may be a differnet action.

Thanks
Scott
 
D

Doug Robbins - Word MVP

Hi Scott

You could set the InputBox up in a Function and call that Function when you
wanted to use it.

Depending upon what it is that you are doing with the macro, there may be
better ways to do it.

Please respond to the newsgroups for the benefit of others who may be
interested.

Hope this helps
Doug Robbins - Word MVP
 
B

BS

Doug,

I can try this but in Word arent the functions and macros tied to a specific
document?

I want this function to work any document, the macro is what changes from
document to document.
In the next document I may not be doing the same thing but I still want the
macro to repeat without
making a new function every time.

For Example:
I may want to take this list of names take out the comma and the space and
add a tab so that it can easily
be imported in to an Access or Excel table.

ABBOTT, RICHARD

ABELN, RONALD E

ACKLEY, SARAH M

ADAMS, ORVILLE M JR

ADAMS, VIVIAN F

AGEE, VERSA L

AGRUSA, HELEN F

AHOLT, IDALA LUELLA
 
D

Doug Robbins - Word MVP

Hi Scott,

If the function is in an Add-In or in the Normal.dot template, it can be
called from a macro in another document. You will need to set up a Public
variable in the Add-In or Normal.dot to hold the result of the InputBox so
that it can be utilised by the macro in the other document.

For example if in the Normal.dot I have

Public myvalue

Public Function ab()
Dim Message, Title, Default
Message = "Enter a value between 1 and 3" ' Set prompt.
Title = "InputBox Demo" ' Set title.
Default = "1" ' Set default.
' Display message, title, and default value.
myvalue = InputBox(Message, Title, Default)
End Function

and in a document, I have a macro containing the following code

ab
MsgBox myvalue

when the macro in that document is run, the InputBox is displayed and after
I enter a number in the InputBox and press enter, a MsgBox opens in the
document containing the number that I entered in the InputBox.

For the example you have given however, you would be a lot better off to use
Word's Edit>Replace facility with

,<space>

in the Find what control, and

^t

in the Replace with control, where for <space> you press the spacebar once.

Then you do not have to know how many entries there are.

You should also see the article “Finding and replacing characters using
wildcards” at:

http://www.mvps.org/word/FAQs/General/UsingWildcards.htm
to get an idea of how powerful Word's Edit>Replace facility is.

For example, it can be used in a macro as follows to round all of the
numbers in a document

' Macro to round all numbers in a document
' Macro created 19/7/00 by Doug Robbins
'
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="[0-9]{1,}.[0-9]{3,}", MatchWildcards: =
True, Wrap:=wdFindContinue, Forward:=True) = True
Selection.Range.Text = Round(Selection.Range.Text, 2)
Loop
End With

Please respond to the newsgroups for the benefit of others who may be
interested.

Hope this helps
Doug Robbins - Word MVP
 

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