[en] Fix errors when executing scripts with VBScript or Python variables

La traduction officielle n'est pas encore disponible.

[en] Problem

[en] You write VBscrip or Python code using label variables. When your NiceLabel solution runs the script, you get an error message about missing variables or values not being set.

[en] The error occurs because of optimization and performance reasons. Only initialized variables load when you run NiceLabel applications. All other variables have no values so in some cases, scripts can't execute. In Automation, your script often uses variables from triggers' data which are also not initialized.

[en] Solution

[en] By default, NiceLabel variable values load conditionally and variables are not used/called until later in runtime. Designer PowerForms initializes label variables and script variables but not the variables used in forms. Automation initializes only script variables.

[en] With these solutions, you initialize all your variables at the beginning of script executions.

[en] Solution 1

Note

[en] This method applies to all your NiceLabel solutions and scripts. If you want only selected scripts to initialize all variables (because of performance reasons), see Solution 2 or Solution 3.

[en] Edit product.config XML file:

  1. [en] Navigate to the NiceLabel System folder.

    [en] %PROGRAMDATA%\NiceLabel\NiceLabel NiceLabel 10

  2. [en] Create a backup copy of product.config file.

  3. [en] Open product.config file in a text editor. The file has an XML structure.

  4. [en] Add the element IntegrationService/UseAllVariablesForScripting with value True.

    [en] The file should have the following contents:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
        ...
        <IntegrationService>
            <UseAllVariablesForScripting>True</UseAllVariablesForScripting>
            ... 
        </IntegrationService>
        ...
    </configuration>
  5. [en] Save and close product.config file.

[en] Solution 2
  • [en] For VBScript:

    [en] Add the comment at the beginning of your script:

    [en] 'NL-LoadAllVariables

  • [en] For Python:

    [en] Add the comment at the beginning of your script:

    [en] #NL-LoadAllVariables

    Exemple 1. [en] Example:

    [en] Code that causes an error:

    [en] list_invert.Value = ",".join(str(x) for x in [y for y in [z for z in range(1, int(last_row.Value) + 1)] if y not in [int(element) for element in selected_rows.Value.split(",")]])

    [en] Variables last_row and selected_rows do not have values until later in execution.

    [en] Code that works:

    [en] #NL-LoadAllVariables

    [en] #Keep comment above to pre-load all NiceLabel variables

    [en] list_invert.Value = ",".join(str(x) for x in [y for y in [z for z in range(1, int(last_row.Value) + 1)] if y not in [int(element) for element in selected_rows.Value.split(",")]])


[en] Solution 3

[en] Manually initialize your variables.

[en] Variable123.Value = Variable123.Value

[en] or

[en] v123 = int(Variable123.Value)

Exemple 2. [en] Example

[en] Code that causes errors:

[en] list_invert.Value = ",".join(str(x) for x in [y for y in [z for z in range(1, int(last_row.Value) + 1)] if y not in [int(element) for element in selected_rows.Value.split(",")]])

[en] Variables last_row and selected_rows do not have values until later in execution.

[en] Code that works:

[en] last_row.Value = last_row.Value

[en] selected_rows.Value = selected_rows.Value

[en] list_invert.Value = ",".join(str(x) for x in [y for y in [z for z in range(1, int(last_row.Value) + 1)] if y not in [int(element) for element in selected_rows.Value.split(",")]])

[en] or

[en] lr = int(last_row.Value)

[en] sr = selected_rows.Value

[en] list_invert.Value = ",".join(str(x) for x in [y for y in [z for z in range(1, lr + 1)] if y not in [int(element) for element in sr.split(",")]])