Defining and Using Global Variables |
The Functions and Subs script, available when using Event Control Scripts, can be used to declare variables that can be accessed by multiple scripts for a single data source. However, because of the way Visual Basic Script works,
If all you need to do is share the output of a script with multiple objects on a template, define a share name for the script that you want to share. Then double-click on any of the other objects whose data you want to come from your shared script and specify the same share name. When you are done setting up the sharing, you will notice that each of the associated data sources will contain the same script in the Data Source property page. (When using multi-line scripts, don’t forget that you won’t have any output to share unless you begin the last line of your script with "value =" and then follow that with the output value that you want to share.)
The sharing of script output between objects on a template (as covered in the previous paragraph) does not address the possible need to access and modify a variable from within the scripts of multiple objects. However, true "global" variables are not supported by
|
The following procedure defines a single object with one data source for each desired global variable. However, by enabling Do Not Print Object, we effectively remove it from the printed item. If you are distracted by the on-screen version, you can always use your mouse to move it off the template. Or, if you prefer, you can change the color of the text to white. Just remember where you put it, so you don’t lose it! Note that, although we give each data source a share name, it is not necessary to have another object share this data in order to reference the share name from your script. |
Add a new text object to the template.
Double-click on the text object to open the Text Properties dialog. Alternatively, select Properties from the object's context menu.
Select the object from the list of available properties pages to display the Selected Object property page.
Enable the option Do Not Print Object.
Select the desired data source from the Navigation Pane.
Select the Data Source tab on the Data Source property page.
Next to the Name field, click the icon to open the Change Data Source Name Wizard.
In the Name field, enter the desired name by which you wish to refer to the selected data source.
Click OK to apply the name change and close the Change Data Source Name Wizard.
On the Data Source property page, replace "Sample Text" with the value to which you would like to initialize your variable.
|
Your variables will only be initialized to the embedded data when you first load the document. If the value stored in the variable is changed by the execution of your scripts, and you need to have a known starting value at the beginning of a print job, you will have to use Event Control Scripts and the OnPrintStart event to initialize your variables. |
Since you can access and/or modify any named data sources from inside of any of your Visual Basic Scripts, they are effectively global variables, even though they are not defined using the conventional Visual Basic technique.