WinCC Explorer · VBScript · Practical Lab

WinCC VB Scripting Fundamentals

Learn the core WinCC VBS workflow: choose the correct event, declare variables, access HMIRuntime, read and write tags, handle Runtime errors and test scripts without bypassing PLC protection.

Beginner-friendly syntax HMIRuntime tag examples Safe write workflow Hands-on lab

Lab Overview

Lab 1: FundamentalsEstimated time: 60 minutesDifficulty: Beginner

Prerequisites / What You’ll Need

  • WinCC Explorer V7.x or V8.x test project
  • Graphics Designer and Global Script (VBS) access
  • One internal test tag and one approved PLC test tag
  • A project backup and Runtime test permission
Quick answer

WinCC VBScript is an event-driven automation layer for HMI behavior. A script normally obtains a tag through HMIRuntime.Tags("TagName"), calls Read, uses Value, or calls Write with a validated value.

  • Use Option Explicit and declare every variable.
  • Keep object-event scripts short and move repeated logic into reusable procedures.
  • Treat every HMI write as an operator request; PLC interlocks remain authoritative.

Learn WinCC VB Scripting from First Principles

This tutorial targets WinCC VB Scripting fundamentals, HMIRuntime Tags examples, read and write WinCC tags with VBScript and WinCC Global Script VBS.

1. What Is WinCC VBScript?

VBScript is a lightweight scripting language available in classic SIMATIC WinCC for custom Runtime behavior. It can connect an object event to tag operations, calculations, navigation, text handling, logging or approved external interfaces when standard dynamics are not sufficient.

Keep responsibilities separate: WinCC scripts support visualization and operator workflow. Sequence control, equipment protection, permissives and safety logic must remain in the approved PLC or safety system.

2. Where WinCC VBS Actions Run

LocationTypical triggerBest use
Graphics Designer object eventMouse click, property or picture eventShort screen-specific operator actions
Global Script VBS actionConfigured event or cyclic triggerReusable project logic and centralized actions
Picture open/close eventPicture lifecycleInitialize or release screen-specific state

The configured trigger determines frequency. Do not place slow file, database or network operations in fast cyclic actions or frequently evaluated object properties.

3. VBScript Syntax Fundamentals

Option Explicit

Dim tagName
Dim currentValue

tagName = "Demo_Temperature"
currentValue = 0

If currentValue >= 80 Then
    HMIRuntime.Trace "Temperature is high: " & CStr(currentValue)
End If

VBScript is case-insensitive, uses Dim for variables, & for safe string concatenation and block statements such as If ... Then ... End If. Option Explicit makes misspelled variable names fail early instead of silently creating a new variable.

4. Read a WinCC Tag with HMIRuntime

Dim temperatureTag
Dim temperatureValue

Set temperatureTag = HMIRuntime.Tags("Demo_Temperature")
temperatureTag.Read
temperatureValue = temperatureTag.Value

HMIRuntime.Trace "Demo_Temperature = " & CStr(temperatureValue)
Set temperatureTag = Nothing

Read updates the tag object before its Value is used. Test with an internal tag first. For an external PLC tag, also verify connection state, tag quality, data type and acquisition settings.

Expected output: WinCC diagnostics show a trace line containing the current test-tag value.

5. Write a WinCC Tag Safely

Dim commandTag
Dim requestedValue

requestedValue = 1

If requestedValue = 0 Or requestedValue = 1 Then
    Set commandTag = HMIRuntime.Tags("Demo_StartCommand")
    commandTag.Write requestedValue
    Set commandTag = Nothing
Else
    HMIRuntime.Trace "Rejected invalid command value"
End If

Validate values before writing and use an internal tag or approved simulation tag during training. A production command should be accepted or rejected by PLC-side mode, permissive and interlock logic. User Administration should also restrict the HMI action to the correct operator role.

6. Build Reusable Subs and Functions

Sub TraceTagValue(ByVal tagName)
    Dim tagObject
    Set tagObject = HMIRuntime.Tags(tagName)
    tagObject.Read
    HMIRuntime.Trace tagName & " = " & CStr(tagObject.Value)
    Set tagObject = Nothing
End Sub

Function IsBinaryValue(ByVal value)
    IsBinaryValue = (value = 0 Or value = 1)
End Function

Use a Sub for an action and a Function when a value must be returned. Pass inputs with ByVal unless the procedure intentionally needs to change the caller variable.

7. Handle Runtime Errors and Always Clean Up

On Error Resume Next

Dim processTag
Set processTag = HMIRuntime.Tags("Demo_Temperature")
processTag.Read

If Err.Number <> 0 Then
    HMIRuntime.Trace "Read failed: " & CStr(Err.Number) & _
        " - " & Err.Description
    Err.Clear
End If

Set processTag = Nothing
On Error GoTo 0

On Error Resume Next must be paired with immediate Err.Number checks. A broad error-suppression block can hide bad tag names and failed external operations. Restore normal handling with On Error GoTo 0.

8. Complete WinCC VBScript Practice Program

Option Explicit

Sub Main()
    Const SOURCE_TAG = "Demo_Temperature"
    Const STATUS_TAG = "Demo_StatusText"

    Dim sourceTag
    Dim statusTag
    Dim processValue
    Dim statusText

    On Error Resume Next

    Set sourceTag = HMIRuntime.Tags(SOURCE_TAG)
    sourceTag.Read

    If Err.Number <> 0 Then
        HMIRuntime.Trace "Cannot read " & SOURCE_TAG & ": " & Err.Description
        Err.Clear
        Set sourceTag = Nothing
        Exit Sub
    End If

    processValue = CDbl(sourceTag.Value)

    If processValue >= 80 Then
        statusText = "HIGH"
    ElseIf processValue >= 50 Then
        statusText = "NORMAL"
    Else
        statusText = "LOW"
    End If

    Set statusTag = HMIRuntime.Tags(STATUS_TAG)
    statusTag.Write statusText

    If Err.Number <> 0 Then
        HMIRuntime.Trace "Cannot write " & STATUS_TAG & ": " & Err.Description
        Err.Clear
    Else
        HMIRuntime.Trace SOURCE_TAG & "=" & CStr(processValue) & _
            ", status=" & statusText
    End If

    Set statusTag = Nothing
    Set sourceTag = Nothing
    On Error GoTo 0
End Sub

Create the two internal tags before testing. Adjust their names only in the constants so spelling remains consistent throughout the script.

9. Performance, Security and Maintainability Rules

  • Prefer standard WinCC dynamics when they solve the requirement without code.
  • Use descriptive tag, procedure and constant names.
  • Avoid blocking dialogs and slow external calls in cyclic or picture-property actions.
  • Do not hard-code passwords or unrestricted database credentials.
  • Trace meaningful failures without flooding diagnostics every cycle.
  • Test the action under the same Runtime account and station role used in production.
  • Keep a versioned project backup and document the event that calls each script.

10. Common WinCC VBScript Errors

SymptomLikely causeAction
Object requiredSet omitted or object creation failedUse Set variable = HMIRuntime.Tags(...) and check the tag name.
Type mismatchText, empty or bad-quality value used as a numberValidate and convert deliberately with CInt, CLng or CDbl.
Script appears not to runWrong event, trigger or Runtime stationAdd a temporary HMIRuntime.Trace at entry and confirm the configured trigger.
Read value never changesMissing Read, bad tag quality or wrong connectionCall Read, inspect tag status and test the external connection.
Write has no process effectWrong tag, insufficient authorization or PLC rejectionCheck the HMI write, user rights, PLC mode, permissives and interlocks.
Runtime becomes slowHeavy work in a fast cycleReduce trigger frequency and move slow I/O out of frequently executed actions.

Hands-On Lab: Read, Classify and Write Internal Tags

Hands-on
Before you start
  • Create internal tags Demo_Temperature and Demo_StatusText.
  • Use a copied training picture, not a production control screen.
  • Open WinCC diagnostics so trace messages are visible.
1

Create the test controls

Add an I/O field for temperature, a text display for status and a button that calls Main.

The picture saves with both internal tags linked.
2

Test all three ranges

Enter values below 50, from 50 to 79.99, and 80 or above, pressing the test button after each value.

Status changes to LOW, NORMAL and HIGH respectively.
3

Prove error reporting

In a backup copy, temporarily misspell a tag constant and run once.

A useful diagnostic trace appears and the action exits without an uncontrolled write.
4

Restore and document

Restore the correct name and record the trigger, tags, expected values and screenshot.

The project is returned to a clean, repeatable test state.

Frequently Asked Questions

Where does VBScript run in WinCC Explorer?

It runs from configured picture-object events or VBS actions in Global Script. The trigger and Runtime station determine when and where it executes.

How do I read and write a WinCC tag?

Obtain the tag with HMIRuntime.Tags, call Read before using Value, and use Write only with a validated value and approved PLC interlocks.

Should I use Option Explicit?

Yes. It catches undeclared and misspelled variables, which prevents many difficult Runtime faults.

Can VBScript replace PLC safety logic?

No. The HMI can request actions, but safety, equipment protection and process interlocks belong in the approved PLC and safety design.

Get the WinCC VB Scripting syllabus

Share your details and a Softwell advisor will contact you with batch dates, fees and project-practice options.

No spam. Used only to share course details for this enquiry.

Build reliable WinCC VBScript projects

Join live online, classroom or corporate industrial automation training.

Request Course Details
Verified learning pathway

Discuss WinCC VB Scripting Training

Explore practical WinCC VBS, SCADA, SQL reporting and industrial automation training options.

Content reviewed: 4 August 2026

☎ Call WhatsApp ✉ Email Enquire Now