How to make a variable persistent?

A

and

Hi,

Does anyone know how to make a variable persistent over a long time?

I have a macro that manipulates words that are selected in a document.
Therefore I retrieve (from the registry or an ini file) the name of a
database file and store this name into a variable.

I prefer to do this only once, when the macro is initiated/started for
the first time, rather that retrieving the database name each time a
word is manipulated.

Any idea how I could achieve this? How I can keep my macro "alive" while
no words are selected or manipulated?

The macro's are written only in "Sub" procedures, there are no functions
and no forms.

Kind regards,

ANDy
 
T

Tom Winter

Dim a module level variable, in a module (as oppossed to class, form, etc.)
OUTSIDE of a Sub/Function/etc. Once set it should retain its value until
Word quits. You could use a document variable if you are working with one
document. Maybe write that name you've gotten back out to an .ini file or a
registry key.
 
J

Jezebel

1) Use a module-level variable as the other poster suggests.

2) Use a static variable within the macro: use 'Static MyVar as whatever'
instead of 'Dim MyVar ...' This is preferable if the value is not needed
anywhere but in the macro.

In both cases, the variable will be cleared if you reset the VBA project.
This happens if you do any work on the code.

3) Attach a document variable to the document itself. May be better if you
have different values for the variable for each document.
 

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