Variable Scope describes when the information in a variable is accessible. When created, the stored value is only available for a certain part of a macro. Macro Express allows for variables with either a Local or Global scope.
Local and Variable Scope
When a variable is created, the ‘Make this Variable Available to Macros Called by this Macro’ option determines whether the variable has a Local or Global scope. Select this option to save the variable with a Global scope. Leave this option unchecked to save the variable with a Local scope.
A Local variable is accessible only inside the macro where it is defined.
For example, suppose you have macros A, B and C, and A is the main macro that calls macros B and C via the Macro Run command. Macro A could have a local variable named %Address%, and macro B could also have a local variable named %Address%. Each of these variables contain separate and distinct values. Changing the variable value in macro B has no effect on the variable in Macro A.
A Global Variable is accessible inside the macro where it is defined and in all other macros called by that macro via the Macro Run command. It is recommended that global variables be defined in the main macro.
For example, suppose you have macros A, B and C and A is the main macro that calls macros B and C via the Macro Run command. After defining a variable named %Address% in macro A both macros B and C can use this variable. Changes made to the value of %Address% in macro B would be available in macros A and B.
Separate variables may be defined with the same name and different scopes. Using the example above, a global variable named %Address% may be defined in macro A and a local variable named %Address% defined in macro C. The global %Address% will be used in macros A and B but the local %Address% will be used in macro C.
Macros are activated in a variety of different ways. Each activation can be thought of as a separate task or thread. This macro thread consists of the main macro and any macros called from that macro by the Macro Run command. If the same macro runs more than once as a result of two separate activations, each activation is running on its own thread.
Variables are not Global between macro threads. This applies even if the same macro is called. For example, suppose you have macros A, B, C and Z. Both macros A and Z call macros B and C via the Macro Run command and both contain the global variable %Address%. The variable %Address% in macro A is separate and distinct from the variable %Address% in macro Z. If macro A sets %Address% to ‘PO Box 106’ and macro Z sets %Address% to ‘120 South Main’ then %Address% in macros B or C would contain ‘PO Box 106’ if called from macro A and ‘120 South Main’ if called from macro Z.
Making information available to different macro threads
Sometimes information needs to be shared between different macro threads. Some examples might include paths to common files, user names, department names, company names or delay values. Each individual or organization will have different needs. There are several effective ways to share values between macro threads.
Use the ‘Variable Modify String: Save to Environment Variable’ command to save a variable to the environment variables area of Windows. Later, use the ‘Variable Set String: Set from an Environment Variable’ command to read an environment variable into a text variable.
Values saved in the environment variable will remain until Macro Express is terminated and are also available to any programs called by Macro Express after the variable has been saved.
Some older versions of Windows may impose a size limit on an individual environment variable or the size of the environment block.
Environment variables are a good place to store text variables. Numeric information may also be stored, but it must be converted to and from a text string. Avoid trying to store control (non-printable) characters in the environment variables.
The ‘Write Registry Value’ and ‘Read Registry Value’ commands may be used to save and restore variables. Saving information in the Registry this way is safe as long as the value is not written to a registry key or value that is used by another program or by the operating system. Also, some users can write to certain areas of the registry and others cannot.
There should not be any problems writing registry values to a registry key similar to this:
HKEY_CURRENT_USER\Software\<Your organization or company name>\Macros
All variable types except control variables may be saved to and restored from the Registry.
Text variables can also be saved in INI files using the ‘Variable Modify String: Save to INI File’ and ‘Variable Set String: Set Value from INI File’ commands.
Values saved in INI files remain until the INI file is modified or deleted. It is possible to store numeric information but it will need to be converted to and from a text string. Avoid trying to store control (non-printable) characters in INI files.
Save anything that can be stored in a text variable to a file using the ‘Variable Modify String: Save to Text File’ command. Later, read this information using the ‘Variable Set String: Set Value from File’ command.
Values saved in text files remain until the file is modified or deleted. It is possible to store numeric information but it will need to be converted to and from a text string. Unlike storing information in environment variables or INI files, control (non printable) characters may be stored in a text file.
Variable Save / Variable Restore
Global variables may be passed from one macro to the next using the Variable Save and Variable Restore commands.