How to read content from a text file

Using the Movicon Basic Scripting engine (VBA or VB.NET) you can manage special functions to enhance your HMI application.  This article covers the steps to read the content from of a .txt file and store it in a Movicon variable to display on the HMI screen.

First step is to create a new Basic Script or add to an existing one, you can right click the Basic Script resource in the Project explorer and select Add a new Script.

This script will need to use the File System Object library to properly work.  This can be enabled by opening the References dialog through the Edit menu and enabling "Microsoft Shell Controls And Automation (1.0)" after finding it in the references list.


There are a couple steps necessary to reach the file that needs to be read.  First is to set the FSO object to reference the FileSystemObject library, then the folder in which to find it and finally the file itself...

Set FSO = CreateObject("Scripting.FileSystemObject")
Set FSOfolder = FSO.GetFolder(sPath)
Set FSOfile = FSO.OpenTextFile(sPath & "\" & sFileName, ForReading) 'set reference for file to read

The full script can be viewed in the example project.

The last step of the script is to place the text that is read from the file into the Movicon variable so it can be displayed on the screen.

TextInFile = sText

Where "TextInFile" is a Movicon string type variable.

Placing the project in runtime mode and pressing the "Read Text File" button on Screen1 will display the text in the white box above the button.

This example script could be managed several ways, for example it could be added to the object itself and run when the screen is loaded.

Please find attached to this article (link at bottom) an example project that highlights this feature.  Simply download the example and extract the project from the zipped folder.


Was this article helpful?
0 out of 0 found this helpful



Please sign in to leave a comment.