How-to
Saving WinCC alarms to disk with scripts
Short answer
WinCC saves alarms to disk through a C-script action — enable the alarm's trigger property and let gmsgfunc write one line per event — or through a VBS export that queries date/time, message number and message text into an Excel or HTML report. Choose event-driven capture for continuous logging and the export for on-demand reports, and verify the runtime account can write to the target folder.

On this page
- How do I save WinCC alarms to disk with a script?
- How do I link a C script to a WinCC alarm?
- What alarm data does the gmsgfunc script receive?
- How do I write the alarm record to a file?
- How do I export WinCC alarm history to Excel or HTML with VBS?
- Why does my alarm script fail to write the file at runtime?
- How do I test that every alarm state reaches the file?
- Questions asked
WinCC gives you two documented ways to put alarms on disk. For continuous capture, select the alarm's script-trigger property in Alarm Logging and let the C action gmsgfunc write one line per alarm event to a file. For a report on demand, run a VBS export that reads the message blocks into an Excel or HTML file. Pick the route that matches the job before you configure anything: the C action runs at the moment each alarm changes, the export reads what the alarm system holds when you call it. Grant the account that runs WinCC Runtime write access to the target folder before the first test.
How do I save WinCC alarms to disk with a script? #
Choose the C-script route when every event of a participating alarm must reach the file the moment it happens, and the VBS export when you need a formatted report of selected blocks on demand; the two need different configuration and can run side by side on one project.
| Route | What starts the write | What the file holds | Built for |
|---|---|---|---|
C-script alarm action (gmsgfunc) |
An alarm event on a message that carries the trigger property | One line per event, written as the event happens | Continuous capture of participating alarms |
| VBS export | You or a schedule runs the script | A report of queried blocks in Excel or HTML output | On-demand and periodic reports |
| Script at the alarm's Event, Incoming point | The analog alarm comes in | Whatever that script writes | WinCC Professional analog alarms, incoming transition |
Neither route is a historical archive on its own. The event route records only alarms that carry the trigger property; the export records only what its query selects, and a query over the current alarm list cannot show events that have already left it. Decide before you write any code which alarms participate and which transitions belong in the file, because that decision fixes the record layout. WinCC Unified is programmed in JavaScript and its alarm interfaces differ, so verify the route in the Unified documentation before carrying any of this into a Unified project.
How do I link a C script to a WinCC alarm? #
In Alarm Logging, select the trigger property on every alarm that must write to disk: classic WinCC labels it 'Triggers an Action', current documentation calls it 'This message triggers an action', and WinCC V8.1 links the script through the 'Loop In Alarm' option in the message's Extended settings.
An attached script without the property is dead code: Alarm Logging fires the action only for messages that carry it, so an alarm missing the selection never writes, never errors and never reaches the file. Set the property first, then attach the script.
The linkage point moves with the release, so match it against the documentation for your installed version. Current WinCC documentation describes linking a C or VB script to a message by activating 'Loop In Alarm' in the message's Properties area, Extended section, then selecting the C scripts or VB scripts tab. Classic projects use the gmsgfunc C function, which the alarm system calls in Runtime for every message carrying 'Triggers an Action'; writing a file changes only what the function does with the message data. One documented Professional pattern places the script at the analog alarm's Event, Incoming position.
What alarm data does the gmsgfunc script receive? #
The alarm system hands the script the message's blocks — date/time, message number and message text at minimum — and your code decides which of them become columns in the file; a block the script never retrieves stays empty no matter what heading you print above it.
Treat every block as a field the script fetches explicitly. Date/time anchors the event, the message number identifies which configured alarm arrived, and the message text carries the wording the operator sees on screen; the vendor's sample export queries exactly these blocks, and you extend it by retrieving any further block your record needs.
Two decisions belong in the record from the start. The transitions are separate message events — occurrence, return to normal, acknowledgment — so add a state field if the file must tell them apart. And label the timestamp: the event time says when the alarm changed, the moment the script writes says when the file grew, and any comparison against another system needs the event time, marked as such. If you later need the same alarm text outside the picture system, the guide to reading WinCC alarm text without the ODK shows where the runtime keeps it.
How do I write the alarm record to a file? #
Write one delimited line per alarm event to an explicit path such as D:\AlarmLogs\AlarmLog.csv, append rather than replace, and close the file after each write so a second alarm arriving mid-write cannot corrupt the record.
Appending and replacing answer different jobs: an event log appends one line per alarm event and keeps everything; a report replaces its output each run. Continuous output needs rotation, because an append-only file otherwise grows without bound.
Worked example — sizing the rotation threshold. Take one example record:
2024-03-14;08:32:07;1001;Pump P-101 seal water low
That line holds 10 + 1 + 8 + 1 + 4 + 1 + 25 = 50 characters plus the line ending, so budget 60 bytes per record. If the process raises 20 alarms in an hour, the file grows by 20 × 60 = 1,200 bytes in that hour and 1,200 × 24 = 28,800 bytes in a day, so a rotation threshold of 1,000,000 bytes is reached after 1,000,000 / 28,800 = 34.7 days, call it 35. A busier unit at 100 alarms an hour reaches the same threshold in 1,000,000 / 144,000 = 6.9 days, call it 7. Rotate at the threshold, archive the closed file, and the log stays bounded at either rate.
Match the format to the reader. Delimited text needs quoting rules for the separators, quotation marks and line breaks that can sit inside message text; HTML output needs the message text escaped so a message carrying markup cannot break the page; and renaming a text file to a workbook extension does not make it a workbook — the example above is a delimited file a spreadsheet opens, nothing more.
How do I export WinCC alarm history to Excel or HTML with VBS? #
For a report, run a VBS export that queries the message blocks — date/time, message number, message text and any block you add code for — from the alarm data and writes the result to an Excel-readable or HTML file; the documented example works through an AlarmControl placed in a picture.
The WinCC scripting manual builds its export example on a WinCC AlarmControl named Control1 sitting in a process picture in Graphics Designer; your project's own control name replaces that. The classic example script queries the message blocks and writes Excel or HTML output, and it returns only the blocks it was written to return — printing a new column heading does not populate the column beneath it.
Build in that order: point the script at a small, known set of alarms, confirm every selected record arrives with its blocks intact, then add the output formatting. Scope the query honestly: an export that reads the current alarm list cannot show events that have already left it, so state in the report what the query covers. Take the example code from the manufacturer's documentation or its authorized distribution channels, and check every call against your installed version before adapting it.
Why does my alarm script fail to write the file at runtime? #
The usual cause is identity: the script writes under the account that runs WinCC Runtime, not your engineering login, so a path that opens in the editor can fail at runtime; give that account write access to the folder and prove it with a test file from Runtime.
Check the second causes in order. A destination on a network share that is briefly unavailable blocks the alarm action while it waits, and an alarm action must stay short — write to a local path or design for the share being down. A target file that someone has open in a spreadsheet fails the append. And if several events can land in the same instant, serialize access so two writes cannot interleave inside one file.
Treat failure as data: capture the write result and surface it through your own diagnostic, because an action that ran is not proof a record reached the disk. Where the failure sits in the alarm reporting chain rather than your script, the guide to why WinCC Runtime deactivates when the alarm report fails walks through that failure mode.
How do I test that every alarm state reaches the file? #
Commission on an isolated runtime with test messages, and check three transitions per participating alarm — occurrence, return to normal and acknowledgment — because each is a separate event and a file holding only number and text cannot tell them apart.
- Record the installed WinCC product, version and runtime PC, and open the matching documentation — the property names and linkage points above move between releases.
- Select the trigger property on every participating alarm and confirm the script is attached at the C scripts or VB scripts tab your version shows.
- Define the record: date/time, message number, message text, state field, delimiter and target path, and whether the file appends.
- Generate one controlled test alarm on an isolated runtime and confirm the line appears with values matching the alarm display. Never trip real equipment to make the test alarm; use test messages or an isolated machine.
- Clear and acknowledge the same alarm and confirm each transition writes its own line with the right state.
- Point the script at an unavailable path under controlled conditions and confirm the failure becomes visible and Runtime stays responsive.
- Run the VBS export against a known set of alarms and confirm every selected record appears and nothing unrelated does.
Decision, restated for each case: continuous capture of participating alarms goes through the trigger property and gmsgfunc; a readable report of selected blocks goes through the VBS export to Excel or HTML; a WinCC Professional analog alarm that only needs its incoming transition attaches its script at the Event, Incoming point. Configure the trigger before the script, prove the writes under the Runtime account, and test all three transitions before the line goes live.
Questions asked
Does the same script route work on a Comfort panel?
No. The gmsgfunc C action belongs to PC runtime, classic WinCC and WinCC Professional. Comfort panels run VBScript, so on a panel you work with the panel's own alarm archive or export functions instead. Verify the exact mechanism in the documentation for your panel and WinCC version before planning the log.
Where can I get a working example of the gmsgfunc alarm script?
From the vendor's own documentation and support channels. The classic sample shows gmsgfunc playing a sound when an alarm occurs, and a file-writing version changes only what the function does with the message data. Take code from the manufacturer's documentation or its authorized distribution channels; third-party download sites are not a safe source for scripts that run inside Runtime.
Why does my export produce duplicate records when I run it again?
Because the script appends a complete report instead of replacing it. Decide one behavior per file: an event log appends one line per alarm event and keeps everything, while a report rebuilds its output from scratch on every run. If an export appends, each run adds the whole previous report again.
Can one C script handle every alarm in the project?
Yes. The trigger property on each message decides participation, not the script: one function runs for every alarm that carries it, and the message data in the call tells your code which alarm arrived. Leave the property off any alarm you do not want in the file.
Does the classic alarm script work in WinCC Unified?
No. WinCC Unified is programmed in JavaScript and its alarm interfaces differ, so a classic C script and the AlarmControl-based export do not transfer one to one. Check the Unified scripting documentation for its alarm functions before you plan either route into a Unified project.
