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, BarTender does not in the conventional sense support variables that are global to an entire document (and therefore shareable by scripts for different objects within a document). By naming a data source using the Data Source property page, the value of a single data source can be shared to other objects and/or data sources using Visual Basic Scripts.

Sharing Script Output Between Objects

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.)

To Define "Global" Variables to be Used by the Scripts of Multiple Objects

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 BarTender in the conventional manner most Visual Basic programmers are familiar with. Instead, you can use one or more data sources with share names as if they are global variables:

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.

  1. Add a new text object to the template.

  2. Double-click on the text object to open the Text Properties dialog. Alternatively, select Properties from the object's context menu.

  3. Select the object from the list of available properties pages to display the Selected Object property page.

  4. Enable the option Do Not Print Object.

  5. Select the desired data source from the Navigation Pane.

  6. Select the Data Source tab on the Data Source property page.

  7. Next to the Name field, click the icon to open the Change Data Source Name Wizard.

  8. In the Name field, enter the desired name by which you wish to refer to the selected data source.

  9. Click OK to apply the name change and close the Change Data Source Name Wizard.

  10. 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.