Understanding Visual Basic Script in Siemens WinCC: A Professional Guide



VBScript or Visual Basic Script is a scripting language designed for the development and implementation of automation and scripting tasks. VB Script is typically used within Human Machine Interface (HMI) environments to automate repetitive operations such as managing tag values, visualization tasks, alarm processing, network error handling, and administrative duties. VBScript is noted for its user-friendly nature, making it a popular choice for industrial control systems. It is based on Microsoft's Visual Basic programming language, focused primarily on automation tasks. Within WinCC, VBScript facilitates the creation of more intuitive graphical user interfaces. WinCC HMI supports VBScript, and this article pertains exclusively to WinCC HMI scripting. This scripting language can be used in either the WinCC software or WinCC TIA Portal.


Adding Components and Tags to Scripts


  1. Open or Create a Project in WinCC Flexible or WinCC TIA Portal.
  2. Select the HMI and add components to your HMI. For this article, insert a button, two input/output fields, and a text field.
  3. After inserting these components, add tags named input1 and input2 and select the UINT data type in the tag table.
  4. Assign the input1 and input2 tags to the input/output fields.

How to Add VBScript in WinCC


To add VBScript in WinCC, go to the project tree, open Scripts, then click "Add New VBScript." The next step is to write the script for the HMI. There are two types of script functions you can add in WinCC:


  • Sub: A Sub is a subroutine function used for executing blocks of code that do not return a value. It performs a series of statements or operations.
  • Function: A Function is used for executing blocks of code that return a value. It's typically employed when you need to perform some calculations or operations and return a result.


In VBScript, open the properties to select the type of script mentioned above. By default, it's set to Sub. The first example is a subroutine function in VBScript, and the second example is a VBScript function without parameters.


How to Write VBScript in WinCC


After adding a script, open the script in WinCC Flexible and write the following code. In this script example, we will only add values for two input/output fields. Here is an example of a subroutine in VBScript:


Sub VBFunction_3()


  • Sub: Subroutine function.
  • VBFunction_3(): The name of the subroutine function, which you can change according to the program.
  • Dim: Declares variables for storing data.

    • value1 and value2 will hold the values of smart tags.
    • result will be used to reference screen items in the HMI (e.g., text fields).
  • SmartTag: Keyword used to read values from tag "input1" and tag "input2".
  • Set: Used to assign an object reference to a variable. Here, result refers to a specific screen item.

    • HmiRuntime.Screens("Screen_2"): Accesses "Screen_2" in the HMI. You can replace "Screen_2" with your screen name.
    • ScreenItems("Text field_1"): Refers to a specific object (text field) on "Screen_2". "Text field_1" is the name of the screen item where the result will be displayed.
  • result.Text: The Text property of the "Text field_1" screen item has been updated.


After creating the script, click to check for script errors. If any errors occur during the error check, remove them before proceeding.


Declaring VBScript in WinCC HMI Components


After creating an error-free script, select a button, open its properties, choose an event, select the button press event, and then choose the script.


Checking Scripts During Simulation


During simulation, you can check how things work in the HMI in real-time. After assigning a script to a button event in the simulation program, perform a test. When the WinCC simulation starts, the values of the input/output fields change. Suppose the input field value is 124 and the other input field value is also 124, then upon pressing the add button, the result of these input/output fields is displayed in the text field.


A Simple Example of a VBScript Function in WinCC


In this simple VB script, the value increments when the increment button is pressed. Each time you click the increment button, the value increases by 1. Therefore, first add a button and rename it to "Increment", then add a numeric input/output field where we display the increment value when the button is pressed. Add a script, and then include the following script in the script editor.


Above is a parameterless VBScript function in WinCC. This VBScript code defines a function named VBFunction_4, which increments the value of a SmartTag named "tag1" by 1 and then returns the updated value. Here is a line-by-line explanation of the VBScript in WinCC:


Function VBFunction_4()


  • This declares the start of a function named VBFunction_4.
  • Functions in VBScript can execute code and must return a value.


SmartTags("tag1") = SmartTags("tag1") + 1


  • This line increments the value stored in the SmartTag with the key "tag1" by 1.
  • SmartTags("tag1"): Refers to the value associated with the tag named "tag1".
  • + 1: Adds 1 to the current value of SmartTags("tag1") and updates it.


VBFunction_4 = SmartTags("tag1")


  • This assigns the updated value of SmartTags("tag1") to the name of the function, VBFunction_4. In VBScript, this is how you specify the return value of a function.


End Function


  • Marks the end of the function.


Declaring VBScript Functions in WinCC


  • After creating the script above, define this VBScript function for a button so that it can be used when the button is clicked. Therefore, first go to the button properties, open the Button Events tab, and select Script. Then declare tag1 in the Process Tags properties tab for the input/output field.