Remebering values from different runs in a macro

P

periodic

I wonder if there is a way to store values in some kind of global variables
for macros between runs as long as the document remains open.

I have seen how to create serial numbers and stuff by storing numbers to
text files. But I wonder if there is a smarter way as long as I have my
document open. In essence I have a macro making multiple runs on the same
document and it should preferably behave a little different depending on how
the run of the previous run ended up.

Regards
Per
 
O

old man

Hi,

The 'old' way was to use a static variable in the procedure. Values assigned
to a static variable keep their values until the document is closed.
sub addit()
static i1 as integer

' i1 will keep its value (or new value) every time addit is run
i1 = i1 + 1

...

exit sub

The new way to maintain a value is to use a variable object (This is
available as of Word 2003 - I am not sure it was available earlier).

ActiveDocument.Variables.Add Name:="i1", Value:="1"

to later check the contents of a variable (which is a part of the variables
collection) use:
ActiveDocument.Variables("i1").Value

old man
 
P

periodic

Ahh thanks, thats just what I was looking for.
I am using word 2003 now so it worked just fine
 
R

Russ

Storing Values When a Macro Ends

http://snipurl.com/swu3

Rerunning code over the same document is tricky. I usually had to have as
part of the code at the beginning to delete bookmarks, headings and footers,
white space, etc., if my code was going to create that stuff again.
Otherwise, those things just grow and grow or complain about being in
existence already.
 

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