Troubleshooting
Why a WinCC Flexible Excel Report Script Overwrites Its Data
Short answer
The script writes Date(), Time() and a SmartTags value into fixed cells A2, B2 and B4, then saves the same workbook, so every trigger replaces the previous report instead of appending a row. A missing or stale report traces to one of three separate failures: tag acquisition, Excel COM automation or file access on the runtime PC.
On this page
The script is doing exactly what it was written to do: it opens the same workbook, writes the current date, time and tag value into fixed cells A2, B2 and B4, saves that one file and quits. Nothing in it appends a row, so every trigger replaces the previous report instead of building history. A missing or stale report, meanwhile, almost always traces to one of three separable failures: the tag value never reached the runtime, Excel automation failed to start, or the runtime account could not open or save the file. Diagnose those three stages separately instead of treating every failed report as a tag problem.
Symptom list and what each one points to #
- No report file after the first trigger. Excel automation, template access or the initial save failed. Open the template file under the runtime account; it must open with no unresolved prompt.
- The report file exists but never changes. The trigger, write access, a workbook lock or the save failed. Check the file's modification time after a trigger; it must advance on every successful save.
- Date and time update but the process value is wrong or stale. Tag selection or acquisition. Read the tag on a live screen and compare it with what cell B4 receives.
- The report seems to lose previous readings. This is the fixed-cell overwrite, not a fault: cells A2, B2 and B4 are replaced each run by design.
- An Excel process stays resident after reporting. Cleanup was skipped because an error interrupted the script before Quit and the object release. The next run then competes with a hidden Excel instance.
How the script actually works #
The script uses COM automation, which lets one application control another's exposed objects. CreateObject("Excel.Application") starts a hidden Excel instance on the runtime PC; CreateObject("Scripting.FileSystemObject") only tests whether the report file exists. This is PC-side application automation, not a report writer built into WinCC flexible. The update path is the part that matters:
Set objExcelApp = CreateObject("Excel.Application")
objExcelApp.Visible = False
objExcelApp.Workbooks.Open "D:\BATCH_REPORT.xls"
objExcelApp.Cells(2,1).Value = Date()
objExcelApp.Cells(2,2).Value = Time()
objExcelApp.Cells(4,2).Value = SmartTags("TE_03_A_B.Value")
objExcelApp.DisplayAlerts = False
objExcelApp.ActiveWorkbook.Save
objExcelApp.Workbooks.Close
objExcelApp.Quit
Set objExcelApp = Nothing
Cells(2,1) is A2, Cells(2,2) is B2, Cells(4,2) is B4, and ActiveWorkbook.Save replaces whatever those cells held. The creation path runs only when D:\BATCH_REPORT.xls is absent: it opens the .xlt template and saves it as the report file. Once the report exists, template edits never reach it, because the existence check no longer fires.
SmartTags("TE_03_A_B.Value") reads whatever value the runtime holds for that tag when the line executes. Whether that value is current depends on the tag's acquisition setting; a tag configured to acquire on use is read at script start, not continuously, which is the same acquisition-timing behavior described in why SmartTags returns the cached value in a Comfort panel script. The script also captures date, time and process value in three separate lines, so they are not an atomic snapshot: a fast-changing tag can sit in B4 alongside a timestamp from a moment later. Where event alignment matters, capture all reporting values before Excel starts, using a defined handshake with the controller.
Checking the runtime PC, not just the project #
The script can be perfect and the report still fail, because Excel automation runs under the account that runs the HMI runtime, not under the account you use for engineering. Three checks, in order:
- Object creation. On the runtime PC, confirm Excel is installed and licensed there, and that the runtime account can launch it. A script that works in an engineering session can fail under the runtime account; an automation failure typically surfaces as an ActiveX component error at the
CreateObjectline, a failure mode documented in Microsoft's answer thread on VBScript Excel automation failing after a system move. - File access. Confirm the destination drive exists and the runtime account can create and modify files there. A workbook already open by hand, on a network share or on the desktop blocks the save.
- Error path. The listing has none: if
OpenorSavefails mid-script, the lines after it never run, leaving Excel resident. Add error handling that records the failed stage, closes the workbook, quits Excel and releases the references before reporting failure.
DisplayAlerts = False suppresses Excel's prompts; it does not fix the operation behind the prompt. During commissioning, resolve file-format and overwrite prompts with alerts visible, then hide the application once the save behavior is proven.
Worked example: proving the overwrite on a batch report #
Inputs: the report file D:\BATCH_REPORT.xls already exists from an earlier batch (created from D:\BATCH_REPORT.xlt); cell B4 holds 83.6, the last batch's temperature from tag TE_03_A_B in degrees C; the current batch reads 91.2 in the runtime; the trigger fires at 14:07:33.
Rule: each trigger opens the existing workbook, writes into fixed cells, and saves. It never adds a row and never opens the template.
Arithmetic: after the trigger, A2 holds the date 2024-05-14, B2 holds 14:07:33, B4 holds 91.2. Row 3 is empty and the previous reading 83.6 appears nowhere in the file. Count of stored readings before the trigger: 1; after the trigger: still 1.
Result: the file modification time advances, so the report "worked", yet the history of batch temperatures was never recorded at all.
Decision: if the report only needs the latest batch's values in a fixed layout, leave the script as it is and treat a missing update as a trigger, lock or save problem. If you need history, change the write target to the next empty row of a table, add a boolean lock against overlapping triggers, and verify with the acceptance sequence below: trigger twice with two different known tag values and require both values to remain present in the saved file.
Acceptance checks on the bench #
- First run: archive any existing report, trigger once, and confirm a new report file appears with the template layout intact and A2, B2, B4 populated.
- Cell mapping: compare B4 with the value displayed live for TE_03_A_B at capture time.
- Repeat run: change the tag through an approved commissioning method, trigger again, and confirm B4 updates in the same file with no historical row added.
- Failure path: in a controlled test, lock the workbook or take the drive offline and confirm the script reports failure rather than a silent success.
- Cleanup: after every run, confirm no Excel process remains resident.
Keep this script for what it is good at: a formatted, single-page snapshot report. For complete, queryable history, log the data through the runtime database rather than Excel; the access path for that is described in reading the WinCC runtime database through the ODBC DSN.
Questions asked
Can the same script keep a history by writing to the next empty row instead of overwriting?
Yes. Find the last used row, for example with the worksheet's used-range property plus one, and write the date, time and tag value into that row's cells before saving. Keep the file open only as long as the write takes, and lock out overlapping triggers with a script boolean, because a second run while Excel still holds the file will fail the open or the save.
Why does the report keep the first tag value even after the process changes?
Two common causes. First, the tag was never read again: the SmartTags expression reads whatever the runtime holds at execution time, so a tag configured for acquisition on use reads at script start, not continuously. Second, the report is being created from the template each time because the report file was deleted or moved, so the save-as path never carries the value you expect. Check the file modification time against the trigger to tell the two apart.
Does the file-existence check reload the template if I edit the report layout?
No. The check only creates D:\BATCH_REPORT.xls from D:\BATCH_REPORT.xlt when the report does not exist. Once the report file exists, the script opens it directly, so template edits never reach an already-created report. To apply a new layout, archive the report file, delete it, and let the script create a fresh copy from the edited template on the next trigger.
Is an Excel-driven script report a good replacement for the HMI runtime database?
It serves different jobs. The script produces a snapshot in a fixed layout for operators or shift paperwork; the runtime database holds continuous logging that you query by time range. If you need complete history with timestamps you can trust, log through the runtime's own data mechanisms and read them back through the database, and keep the Excel script for the formatted one-page report.