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

Re: How to make a variable persistent? by Tom

Tom
Wed Feb 18 12:17:48 CST 2004

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.
--
Tom Winter
Tom@NoSpam.AmosFiveSix.com
www.AmosFiveSix.com

"and" <and@nospam.invalid> wrote in message
news:4033a9b2$0$6051$fb624cd1@morenews.zeelandnet.nl...
>
> 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



Re: How to make a variable persistent? by Jezebel

Jezebel
Wed Feb 18 20:49:32 CST 2004

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.




"and" <and@nospam.invalid> wrote in message
news:4033a9b2$0$6051$fb624cd1@morenews.zeelandnet.nl...
>
> 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