<!-- Softwell WinCC VBScript 20-Part Tutorial · Blog 02 · updated 2026-08-29 --> WinCC VBScript Keywords: Syntax, Uses & Cheat Sheet
Blog 02 · Beginner Foundation · WinCC Explorer VBScript

WinCC VBScript Keywords: Syntax, Uses & Cheat Sheet

Learn the essential WinCC VBScript keywords by category. For each keyword, understand its purpose, where it is applied in WinCC, and the minimum syntax you need before moving to tags, alarms, SQL and reporting.

Keyword categories WinCC applications Minimum syntax Quick cheat sheet

Tutorial 02 Learning Goal

Topic: VBScript KeywordsFormat: Uses + Application + SyntaxDifficulty: Beginner

What to learn from this page

  • Recognize the most important VBScript keywords used in WinCC scripts.
  • Understand what each keyword does and where it is normally applied.
  • Memorize only the minimum syntax pattern, not large example programs.
  • Separate VBScript keywords from operators, functions and WinCC objects.
  • Use the cheat sheet as a quick classroom and troubleshooting reference.

Learning Foundation: VBScript Language Foundation

This page has one defined job in the WinCC VBScript learning path. Master this foundation before using the same concept inside larger SCADA, SQL Server and reporting scripts.

BeginnerWinCC Explorer / Classic WinCCSQF Running Project
Prerequisite

WinCC project can open in Graphics Designer and Runtime. Start with internal tags for safe testing.

Core concept

Learn the syntax vocabulary used everywhere else: comments, Dim, Set, If, For, Sub, Function, object references, dot notation and runtime event procedures.

SQF practical connection

Create simple SQF test variables and message-box checks before reading live process tags.

Expected competency

Read a WinCC VBScript procedure line by line and identify keywords, objects, methods, arguments and event entry points.

WinCC VBScript Keywords: Syntax, Uses & Cheat Sheet — ArchitectureCode-rendered HTML/CSS architecture; no image file required
Event Procedure

WinCC calls your script from an event

Sub OnClick(ByVal Item)
Keywords

Declare and control execution

Dim / If / For
Objects

Reference Runtime components

HMIRuntime
Methods

Perform actions on objects

.Read / .Write
Result

Verify with safe test output

MsgBox

Complete WinCC VBScript Learning Path

Use Previous/Next for the recommended practical order, or open any topic below as a reference.

WinCC VB Scripting Training →
Focus of this chapter

This page is a VBScript keyword reference. It intentionally uses only short syntax fragments. Full programs belong in the later chapters on conditions, loops, procedures, tag Read/Write, SQL and practical WinCC examples.

1. What Is a VBScript Keyword?

A VBScript keyword is a reserved word that forms part of the language grammar. Keywords define how a script declares data, makes decisions, repeats instructions, creates procedures, works with object references and handles runtime errors.

KeywordDim, If, For, Sub
Part of VBScript grammar.
FunctionCStr(), CDbl(), IsNumeric()
Performs an operation and is not a keyword.
WinCC object / methodHMIRuntime, Tags, Read, Write
Provided by the WinCC object model.
Reading rule: In any WinCC VBS script, first identify the VBScript keywords. Then identify variables, operators, functions, WinCC objects and methods. This makes long scripts easier to troubleshoot.

2. WinCC VBScript Keywords — Master Cheat Sheet

This is the main quick-reference table for the chapter.

CategoryImportant keywordsPrimary use in WinCC
Script disciplineOption ExplicitForce variables to be declared and reduce typing mistakes.
DeclarationDim, Const, ReDim, PreserveStore tag values, setpoints, status text, arrays and temporary data.
Object referencesSet, NothingWork with WinCC objects, ADODB objects, Excel objects and file-system objects.
DecisionIf, Then, ElseIf, Else, End IfInterlocks, alarm conditions, validation and status logic.
Multi-state decisionSelect Case, Case, Case Else, End SelectMode selection, equipment states and event codes.
Counted loopFor, To, Step, NextRepeat operations across arrays, tags or fixed indexes.
Collection loopFor Each, In, NextProcess each item in an array or collection.
Conditional loopDo, While, Until, LoopRecordset iteration and condition-controlled repetition.
Loop exitExit For, Exit DoStop repetition when a limit, fault or target condition is reached.
ProceduresSub, End Sub, Function, End Function, CallCreate reusable WinCC script logic.
ParametersByVal, ByRefControl whether procedures receive a copy or can modify the caller variable.
LogicAnd, Or, Not, XorCombine interlocks, permissives and alarm conditions.
Special valuesEmpty, Null, Nothing, True, FalseCheck initialization, database nulls, object references and Boolean states.
Error handlingOn Error Resume Next, On Error GoTo 0Handle recoverable runtime errors around SQL, files, Excel or object access.

3. Declaration & Object Keywords

These keywords prepare the data and object references used by the rest of the script.

1

Option Explicit

Use: Requires every variable to be declared. Application: Recommended for WinCC Global Scripts and larger VBS projects because misspelled variable names become easier to detect.

Option Explicit

Dim TempAct
2

Dim

Use: Declares a variable or fixed-size array. Application: Store process values, setpoints, strings, counters and temporary calculation results.

Dim TempAct
Dim ProcessName(3)
VBScript note: VBScript does not use typed declarations such as Dim TempAct As Double.
3

Const

Use: Declares a named value that should not change. Application: Fixed limits, status codes or configuration values.

Const HighTempLimit = 900
4

ReDim and Preserve

Use: Resize a dynamic array; Preserve keeps its current elements. Application: Build variable-length event or report lists.

Dim Events()
ReDim Events(1)
ReDim Preserve Events(2)
5

Set and Nothing

Use: Set assigns an object reference; Nothing clears it. Application: WinCC tag objects, ADODB connections/recordsets, Excel automation and file-system objects.

Dim TagObj
Set TagObj = HMIRuntime.Tags("Temp_Act")

' ...use TagObj...

Set TagObj = Nothing
Rule: Use Set for objects, not for ordinary numbers or strings.

4. Decision Keywords

Decision keywords control which statements execute according to process conditions, operator commands or equipment states.

KeywordUseWinCC application
If ... ThenTest one Boolean condition.Check alarm, permissive or command condition.
ElseIfTest another condition.Classify a process value into ranges.
ElseFallback path.Default status when no earlier condition is true.
End IfClose the If block.Required for multiline decisions.
Select CaseEvaluate one expression against multiple cases.Mode, state, step or event-code handling.
Case ElseFallback case.Unknown or invalid mode/state.

Syntax: If / ElseIf / Else

If TempAct > 900 Then
    StatusText = "HIGH"
ElseIf TempAct >= 800 Then
    StatusText = "NORMAL"
Else
    StatusText = "LOW"
End If

Syntax: Select Case

Select Case Mode
    Case 0
        ModeText = "STOP"
    Case 1
        ModeText = "MANUAL"
    Case 2
        ModeText = "AUTO"
    Case Else
        ModeText = "UNKNOWN"
End Select

5. Loop Keywords

Loops reduce repetitive statements. In WinCC they are useful for arrays, repeated tag operations, report rows and SQL Recordset processing.

PatternBest useCore keywords
Counted loopKnown number of repetitions.For, To, Step, Next
Array / collection loopProcess every item.For Each, In, Next
Condition loopRepeat while/until a condition changes.Do, While, Until, Loop
Early exitStop loop on fault/target.Exit For, Exit Do

Syntax: For ... Next

For i = 0 To 3
    HMIRuntime.Trace ProcessName(i) & vbCrLf
Next

Syntax: For Each

For Each TagName In TagNames
    HMIRuntime.Trace CStr(TagName) & vbCrLf
Next

Syntax: Do While

Do While Not rs.EOF
    ' Process current SQL row
    rs.MoveNext
Loop

6. Procedure Keywords

Procedures make scripts reusable and easier to maintain. Use a Sub for an action and a Function when a value must be returned.

KeywordPurposeTypical WinCC use
Sub ... End SubCreate a procedure that does not return a value.Write tags, update status, perform an action.
Function ... End FunctionCreate a procedure that returns a value.Validation, scaling, formatting or reusable calculations.
CallExplicitly invoke a procedure.Call reusable routines.
ByValPass a copy of an argument.Protect input parameters from modification.
ByRefPass a reference to the caller variable.Allow a procedure to update the caller variable intentionally.
Exit Sub / Exit FunctionLeave a procedure early.Stop execution after validation or interlock failure.

Syntax: Sub

Sub TraceValue(ByVal NameText, ByVal ValueText)
    HMIRuntime.Trace NameText & " = " & CStr(ValueText) & vbCrLf
End Sub

Syntax: Function

Function IsHigh(ByVal Value)
    IsHigh = (Value >= 900)
End Function

7. Logical & Special-Value Keywords

Logical keywords

KeywordMeaningExample application
AndAll conditions must be true.Start command AND all permissives OK.
OrAt least one condition is true.Any trip source generates a common fault.
NotInvert a Boolean condition.Run only when fault is NOT active.
XorTrue when conditions differ.Less common; useful for mutually exclusive states.
If StartCmd And InterlockOK And Not FaultActive Then
    PermitStart = True
End If

Special-value keywords

KeywordMeaningTypical check
True / FalseBoolean values.Command, alarm or interlock state.
EmptyVariant has not yet been initialized.Check an unassigned variable.
NullContains no valid data value.Common when reading database fields.
NothingNo object reference is assigned.Release or test object variables.

8. Error-Handling Keywords

VBScript does not use Try...Catch. WinCC scripts commonly use the On Error statements around operations that may fail, then inspect the built-in Err object.

Syntax

On Error Resume Next

' Operation that may fail

If Err.Number <> 0 Then
    HMIRuntime.Trace "Error " & CStr(Err.Number) & ": " & Err.Description & vbCrLf
    Err.Clear
End If

On Error GoTo 0
Application: Especially useful around ADODB connections, SQL commands, file access, Excel automation and other external objects. Do not use On Error Resume Next to hide errors without checking Err.Number.

9. Keywords vs Functions vs WinCC Objects

This distinction is important because not every familiar VBScript-looking word is a keyword.

ExampleTypeRole
Dim, If, For, SubVBScript keywordsDefine language structure.
CStr(), CDbl(), IsNumeric(), Replace()Built-in functionsConvert, test or manipulate values.
vbCrLf, vbInformationBuilt-in constantsProvide predefined values.
HMIRuntimeWinCC Runtime objectEntry point to WinCC Runtime services.
HMIRuntime.Tags("Temp_Act")WinCC object accessObtain a WinCC tag object.
.Read, .Write, .TraceWinCC methodsPerform WinCC-specific actions.

10. WinCC VBScript Keyword Application Map

Use this map to decide which keyword group is relevant before writing a script.

WinCC taskMost useful keywordsNext detailed chapter
Declare process variablesOption Explicit, Dim, ConstVariables & Data Conversion
Alarm / limit decisionIf, ElseIf, ElseIf, ElseIf & Select Case
Mode / state selectionSelect Case, CaseDecision Logic
Process arrays / repeated tagsFor, For Each, Do WhileLoops & Arrays
Create reusable logicSub, Function, ByVal, ByRefSub & Function
Read / write WinCC tagsDim, Set, decisions as requiredHMIRuntime Tag Read/Write
SQL / Excel / file automationSet, loops, On Error, NothingSQL Server CRUD

SQF Project Keyword Recognition

The purpose here is only to identify keyword roles inside a typical WinCC fragment—not to introduce a full project program.

On Error Resume Next

Dim TempAct
Dim TagObj

Set TagObj = HMIRuntime.Tags("Temp_Act")
TempAct = TagObj.Read

If TempAct >= 900 Then
    HMIRuntime.Trace "High temperature" & vbCrLf
End If

Set TagObj = Nothing
On Error GoTo 0
Keywords to identify: On Error, Resume Next, Dim, Set, If, Then, End If, Nothing and GoTo 0. HMIRuntime, Tags, Read and Trace are WinCC object-model items, not VBScript keywords.

Frequently Asked Questions

What are the most important VBScript keywords for WinCC beginners?

Start with Option Explicit, Dim, Const, Set, If, ElseIf, Else, Select Case, For, Do, Sub, Function, And, Or, Not, Nothing and the On Error statements.

Is HMIRuntime a VBScript keyword?

No. HMIRuntime is a WinCC Runtime object. VBScript keywords define the language structure around it.

When should Set be used?

Use Set when assigning an object reference. Do not use it for ordinary numeric or string values.

Which keywords are best for WinCC mode or state logic?

Select Case is usually clear when one variable represents several modes or states. If...ElseIf...Else is better when conditions are based on ranges or multiple Boolean expressions.

Which keywords are used for SQL Recordset iteration?

A common pattern is Do While Not rs.EOF ... rs.MoveNext ... Loop. The detailed ADO and SQL implementation is covered in the SQL chapters.

Next: variables, operators and data conversion

After the keyword grammar is clear, continue to how VBScript stores, compares and converts WinCC process data.

Continue to Blog 03 →
Verified learning pathway

Discuss WinCC VB Scripting Training

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

Content reviewed: 28 August 2026

☎ Call WhatsApp ✉ Email Enquire Now