WinCC VBS Indirect Addressing: Dynamic Tag Reading via HMIRuntime

David Krause10 min read
SiemensTutorial / How-toWinCC
Licensed PE Working through this on a live machine? A Maine-licensed engineer can take it from here — included with IMD hardware, by the hour for everything else. Book an engineer

Overview

WinCC V6.2 and later versions expose a powerful set of COM-like automation objects that allow VBScript (VBS) to read, write, and even modify the configuration of process tags at runtime. Indirect addressing — where one internal text tag stores the name (or the address) of a second tag — is the standard pattern for building reusable HMI screens, dashboard frameworks, and tag-agnostic diagnostic overlays.

This reference documents two distinct indirect-addressing approaches for WinCC VBS:

  1. HMIRuntime.Tags pointer method — read a tag whose name is stored as a string in another tag. Best for read-only or one-shot read/write scenarios in picture scripts.
  2. HMIGenObjects.HMIGO address method — physically reassign a tag's S7/S5 address at runtime. Use when the same configured tag must follow a dynamic PLC variable without rebinding every I/O field.

The tutorial targets WinCC V6.2 SP3 or higher (SP4/SP5/SP6 in modern installations), but the VBS object model is stable up through WinCC 7.x. All code samples are written for the WinCC VBS editor and execute inside picture events, global actions, or scheduled tasks.

Engineering note: Runtime configuration via HMIGenObjects requires that the WinCC project be launched with an account that belongs to the configured WinCC Administrator group. Picture-level HMIRuntime scripts run under the operator account and should not need administrator rights.

Prerequisites

Requirement Detail
WinCC version V6.2 SP3 minimum; V7.0 / V7.4 / V7.5 also supported
Project setting Global Script Runtime enabled in Computer properties
Tag type Internal text tag (Text 8-Bit for ASCII) or Text 16-Bit (for Unicode tag names)
Authorization Operator-level read/write on internal tags; admin-level for runtime address changes
Engineering environment WinCC Explorer open with Graphics Designer for screen binding tests

Indirect Addressing Concepts in WinCC

WinCC supports three levels of indirection for I/O fields, bar graphs, and online table controls:

Level Mechanism Limitations
Direct Tag name hard-bound to graphic object property One tag per property
Indirect via property dialog Checkbox Indirect in I/O field properties; tag name resolved at runtime from a "pointer" text tag String-tag pointer only; cannot XOR
VBS indirect Script constructs the tag reference via HMIRuntime.Tags(...) Requires VBS; full read/write control

The dialog-driven indirect addressing is fast but limited to a single read. VBS indirect addressing adds the ability to perform arithmetic, bit operations (XOR, AND, OR, shift), concatenation, and conditional logic on the dereferenced value before displaying or writing it back.

Configuring an Indirect Text Tag

Before any VBS dereferencing is possible, an internal text tag must exist in the project. Recommended defaults are summarized below.

Property Recommended Value Reason
Name TagPointer or Temp Convention: indicates role
Type Text variable, 8-Bit (ASCII) or 16-Bit (Unicode) 8-Bit covers ASCII tag names; 16-Bit covers Unicode names with non-ASCII characters
Length 32 chars (8-Bit) / 16 chars (16-Bit) Accommodates fully-qualified connection-prefixed names
Update On change Standard; avoid 250 ms polling unless pointer updates often
Initial value Leave blank Empty pointer returns SCRIPT ERROR #22 in VBS

Bind this tag to the I/O field Output Value property of an I/O field on the start picture. Operators will type the name of the actual process tag they want to inspect; the VBS action consumes the string and dereferences it.

HMIRuntime.Tags — Pointer-Based Indirect Read

The standard dereferencing pattern is a four-step sequence: obtain the HMIRuntime tag wrapper, read the pointer value, build a new tag wrapper for the dereferenced name, then read or write the dereferenced tag.

Step 1 — Dereference and copy

' WinCC VBS action triggered by I/O field "OutputValueChanged" or "OnClick"
Dim objPointer, strTarget, objTarget, varValue, objResult

' 1. Read the pointer tag (Text 8-Bit) configured in the project
Set objPointer = HMIRuntime.Tags("Temp")
objPointer.Read
strTarget = objPointer.Value

' 2. Defensive: empty pointer is a script error — abort cleanly
If Len(strTarget) = 0 Then
    HMIRuntime.Trace "Indirect address: pointer tag is empty, aborting." & vbNewLine
    Exit Sub
End If

' 3. Open the dereferenced tag
Set objTarget = HMIRuntime.Tags("" & strTarget & "")
objTarget.Read
varValue = objTarget.Value

' 4. Write result into a stable holding tag the I/O field can display
Set objResult = HMIRuntime.Tags("ResultTag")
objResult.Write varValue

Step 2 — Parameter mapping

Argument Type Description
HMIRuntime Global Object Always-on root object provided by WinCC runtime; do not declare it
.Tags(name) Function Returns an ITag interface wrapper. The name may be unqualified (internal) or fully qualified as Connection::TagName
.Read Method Fetches current value into the wrapper. Synchronous in VBS, blocks until the underlying driver returns
.Write value Method Sends the value back to the PLC or internal store
.Value Property Variant holding the read or pending-write payload

Step 3 — Connection-qualified dereferencing

If multiple connections exist, fully qualify the dereferenced tag name to disambiguate:

Set objTarget = HMIRuntime.Tags("S7_400_Station1::Motor_Speed")

The :: separator follows the WinCC naming convention <Connection>::<Tag>. Without a connection prefix WinCC walks the default search order and may raise Script Warning 0x80040402 ("Tag not found").

HMIGenObjects.HMIGO — Runtime Tag Readdressing

For situations where the project is pre-loaded with a tag stub (e.g., DynamicTag) that must follow whatever PLC address the operator selects, use the HMIGenObjects COM object. This method rewrites the tag's TagS7S5Address property — the address of the Simatic S7/S5 target — and commits the change live.

' Reusable WinCC VBS function
Function SetAddress(ByVal TagName, ByVal Address)
    Dim HG
    Set HG = CreateObject("HMIGenObjects.HMIGO")

    If (HG Is Nothing) Then
        HMIRuntime.Trace "SetAddress: HMIGO object unavailable." & vbNewLine
        Exit Function
    End If

    HG.GetTag TagName           ' Load tag configuration into the object
    HG.TagS7S5Address = Address ' Assign new DB/byte/bit address
    HG.CommitTag                ' Persist the change to the runtime database

    Set HG = Nothing
End Function

Property reference for HMIGO

Member Read/Write Description
GetTag(name) — Loads an existing tag. Throws COM error 0x80040403 if the tag does not exist
TagS7S5Address R/W Address string in WinCC notation: e.g. DB100.DBW20, DB100.DBX2.0, MW40, E0.7
TagName R/W Edit the tag name (rename) without losing history
TagType R/W 0=Binary, 1=Byte, 2=Word, 3=Short Int, 4=Long Int, 5=Float, 6=Double, 7=Text
CommitTag — Persists all pending edits to the runtime database. Without this call the change is local to the object
Limitation: HMIGenObjects.HMIGO operates only on the runtime tag table copy. Persisting into the offline configuration requires executing the action inside the WinCC Explorer (Configuration mode) or using the WinCC ODK API. Readdressing during a running session is volatile if WinCC is restarted unless the project was opened with Tag Persistence enabled.

Practical Use Case — Bit Toggle via XOR

The classic operator scenario: a button toggles a single boolean bit in the PLC using an exclusive-OR mask of 0x01. With indirect addressing, the same picture object can toggle any selected bit on any selected tag.

' Picture event "OnClick" of a Toggle button
Dim objPtr, strTarget, objTarget, varRaw, varToggled

Set objPtr = HMIRuntime.Tags("Temp")
objPtr.Read
strTarget = objPtr.Value

If Len(strTarget) = 0 Then Exit Sub

Set objTarget = HMIRuntime.Tags("" & strTarget & "")
objTarget.Read
varRaw = objTarget.Value

' XOR with 1 to flip the least-significant bit
varToggled = CLng(varRaw) Xor &H1

' Bit-aware write: dispatch based on the tag's logical type
If (objTarget.TagType = 0) Then       ' Binary
    objTarget.Value = (varToggled And &H1)
Else
    objTarget.Value = varToggled
End If

objTarget.Write

To toggle a bit at an arbitrary position inside a word:

Dim lBitPos, lMask, lValue, lNewValue
lBitPos  = 5
lMask    = (2 ^ lBitPos)         ' 0x20 in this example
lValue   = CLng(objTarget.Value)
lNewValue = lValue Xor lMask     ' flip bit 5 only
objTarget.Value = lNewValue
objTarget.Write

Error Handling and Defensive Coding

WinCC VBS exposes no structured exception mechanism; failures fall back to On Error Resume Next. Place a single, project-wide error trap at the top of every action or picture script.

On Error Resume Next

Dim sErr, lErr
Set objPtr = HMIRuntime.Tags("Temp")
objPtr.Read

lErr = Err.Number
If lErr <> 0 Then
    sErr = Err.Description
    HMIRuntime.Trace "Indirect read failed at Temp: " & lErr & " - " & sErr & vbNewLine
    Err.Clear
    Exit Sub
End If

Common WinCC VBS error codes

Hex Decimal Meaning Typical cause
0x80040402 -2147410942 Tag not found Pointer tag contains a non-existent name
0x80040403 -2147410941 GetTag: tag does not exist HMIGO target name typo
0x80040410 -2147410928 Connection lost Underlying driver channel down
0x80004005 -2147467259 Unspecified COM failure Permission denied on HMIGO write
0x80070057 -2147024809 Invalid argument Malformed TagS7S5Address string

Verification Procedure

  1. Open the Graphics Designer and place an I/O field on the start picture. Bind its Output Value to ResultTag (configured as a signed 32-bit Binary tag).
  2. Bind a second I/O field's Output Value to the pointer tag Temp (Text 8-Bit). Mark the field as Output.
  3. Attach the indirect-read VBS action to the OnChange or OnClick event of the pointer I/O field.
  4. Start WinCC Runtime (RT) and enter a known good tag name (e.g., PLC1::MotOn) into the pointer field.
  5. Confirm in WinCC Explorer → Tags → Tag Management that the value displayed in the ResultTag I/O field matches the live PLC value.
  6. To verify HMIGO re-addressing: enter a new address in the VBS, click the toggle button, and confirm the dynamic read now returns the new DB area.
  7. Capture activity by enabling Tag Logging on the picture or by tailing WinCCdiag_Trace from HMIRuntime.Trace lines.

Troubleshooting Matrix

Symptom Likely Cause Resolution
I/O field shows "####" Result tag type does not match returned value Set ResultTag to Binary or change tag TagType accordingly
Script aborts silently on first run Empty pointer tag Assign a default target tag in project initialization
HMIGO CommitTag fails with access denied Operator user lacks WinCC admin rights Add user to WinCC Administrator group, or restrict re-addressing to engineering mode
Toggled bit does not flip after click Tag mapped to Input only Change tag direction to Read/Write in Tag Management
XOR writes zero Tag type is Binary and raw value is not numeric Convert with CInt or CLng before XOR
Tag lookup resolves to wrong connection Tag name present in multiple connections Always qualify with Connection::TagName
Runtime re-addressing loses change after restart Tag persistence disabled Open project properties → Tag Persistence → Enable
Unicode tag names not resolved Pointer tag is Text 8-Bit Switch pointer tag to Text 16-Bit (Unicode)
XOR works but other picture objects keep old value No Update cycle triggered Set update cycle to On change and call objTarget.Write explicitly

Performance and Security Notes

  • Cycle placement. Schedule dereferencing actions on the picture's OnClick or tag OnChange event, not on the 250 ms cycle. Each call retrieves the latest value through the WinCC data manager — high-frequency scripts can saturate the channel.
  • Caching wrappers. HMIRuntime.Tags(name) always allocates a new interface. If the same tag is dereferenced in a loop, store the wrapper once and reuse .Read / .Write.
  • Operator privilege. HMIRuntime exposes all configured tags regardless of authorization unless picture-level Authorizations are set on the screen. Industrial deployments should always enable WinCC User Administrator and bind sensitive screens to an authorization level (e.g., "Operator level 5 – Maintenance").
  • Address validation. Validate any operator-supplied address before passing it to HG.TagS7S5Address. An invalid address not only fails to commit but may corrupt the in-memory tag configuration. Use a regex such as ^(DB\d+\.DB[XBWD]\d+(\.\d+)?|M[WBXD]\d+(\.\d+)?|[EIA]\d+(\.\d+)?)$ before assigning.
  • Audit trail. Every HMIRuntime.Trace call is captured to the diagnostics log when WinCCdiag is enabled, providing an audit trail for re-addressing events in regulated environments.

FAQ

What is the difference between WinCC indirect addressing in the I/O field dialog and VBS indirect addressing?

Dialog-driven indirect addressing binds a single text tag as the pointer and shows the dereferenced value directly. VBS indirect addressing lets you dereference, perform operations such as XOR, scaling, or unit conversion, and write the result anywhere. Use VBS for any logic beyond a straight read.

Why does HMIRuntime.Tags("<name>") return Script Error 0x80040402?

The error means the name passed to Tags() is not present in the runtime tag table. Verify spelling, check that the connection prefix is included when multiple connections exist, and ensure the project has been downloaded to runtime (Save As > Compile OS).

Can HMIGenObjects.HMIGO change a tag's address while WinCC Runtime is active without a restart?

Yes. Call GetTag, assign the new value to TagS7S5Address, and execute CommitTag. The change takes effect on the next tag acquisition cycle. Note that the change is volatile unless Tag Persistence is enabled in the project properties.

How do I toggle bit 5 of a Word tag using VBS?

Read the value with objTarget.Read, convert via CLng, mask with (2 ^ 5), compute lValue Xor lMask, then objTarget.Write. The example "Practical Use Case — Bit Toggle via XOR" section shows the complete pattern for both Binary and Word tags.

Is HMIRuntime.Tags the same object on WinCC V6.2 and WinCC V7.x?

Yes. HMIRuntime and its .Tags, .AlarmLog, and .Trace members are stable from WinCC V6.0 through WinCC V7.5 SP2. Code written for V6.2 generally runs unchanged on V7.x; only the picture/script editor dialogs were restructured.

Back to blog