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 Explicitand 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.
2. Where WinCC VBS Actions Run
| Location | Typical trigger | Best use |
|---|---|---|
| Graphics Designer object event | Mouse click, property or picture event | Short screen-specific operator actions |
| Global Script VBS action | Configured event or cyclic trigger | Reusable project logic and centralized actions |
| Picture open/close event | Picture lifecycle | Initialize 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 IfVBScript 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 = NothingRead 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.
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 IfValidate 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 FunctionUse 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 0On 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 SubCreate 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
| Symptom | Likely cause | Action |
|---|---|---|
| Object required | Set omitted or object creation failed | Use Set variable = HMIRuntime.Tags(...) and check the tag name. |
| Type mismatch | Text, empty or bad-quality value used as a number | Validate and convert deliberately with CInt, CLng or CDbl. |
| Script appears not to run | Wrong event, trigger or Runtime station | Add a temporary HMIRuntime.Trace at entry and confirm the configured trigger. |
| Read value never changes | Missing Read, bad tag quality or wrong connection | Call Read, inspect tag status and test the external connection. |
| Write has no process effect | Wrong tag, insufficient authorization or PLC rejection | Check the HMI write, user rights, PLC mode, permissives and interlocks. |
| Runtime becomes slow | Heavy work in a fast cycle | Reduce trigger frequency and move slow I/O out of frequently executed actions. |
Hands-On Lab: Read, Classify and Write Internal Tags
Hands-on- Create internal tags
Demo_TemperatureandDemo_StatusText. - Use a copied training picture, not a production control screen.
- Open WinCC diagnostics so trace messages are visible.
Create the test controls
Add an I/O field for temperature, a text display for status and a button that calls Main.
Test all three ranges
Enter values below 50, from 50 to 79.99, and 80 or above, pressing the test button after each value.
Prove error reporting
In a backup copy, temporarily misspell a tag constant and run once.
Restore and document
Restore the correct name and record the trigger, tags, expected values and screenshot.
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.
