How to read an OPC item value in Excel: Difference between revisions
(Created page with "This is the simplest Excel VBA example. It reads an OPC item value and stores it in a cell of a worksheet. Step 1: Create a new Excel worksheet, and select Developer -> Visua...") |
No edit summary |
||
Line 12: | Line 12: | ||
Step 4. Enter the code for the handler as follows: | Step 4. Enter the code for the handler as follows: | ||
Private Sub Workbook_Open() | <span style='color:#800000; font-weight:bold; '>Private</span> <span style='color:#800000; font-weight:bold; '>Sub</span> Workbook_Open<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span> | ||
' Create EasyOPC-DA component | <span style='color:#696969; '>' Create EasyOPC-DA component</span> | ||
Dim EasyDAClient As New EasyDAClient | <span style='color:#800000; font-weight:bold; '>Dim</span> EasyDAClient <span style='color:#800000; font-weight:bold; '>As</span> <span style='color:#800000; font-weight:bold; '>New</span> EasyDAClient | ||
' Read item value and display it | <span style='color:#696969; '>' Read item value and display it</span> | ||
Range("A1").Value = EasyDAClient.ReadItemValue("", "OPCLabs.KitServer.2", "Demo.Single") | Range<span style='color:#808030; '>(</span><span style='color:#0000e6; '>"A1"</span><span style='color:#808030; '>)</span><span style='color:#808030; '>.</span>Value <span style='color:#808030; '>=</span> EasyDAClient<span style='color:#008c00; '>.</span>ReadItemValue<span style='color:#808030; '>(</span><span style='color:#0000e6; '>""</span><span style='color:#808030; '>,</span> <span style='color:#0000e6; '>"OPCLabs.KitServer.2"</span><span style='color:#808030; '>,</span> <span style='color:#0000e6; '>"Demo.Single"</span><span style='color:#808030; '>)</span> | ||
End Sub | <span style='color:#800000; font-weight:bold; '>End</span> <span style='color:#800000; font-weight:bold; '>Sub</span> | ||
As you can guess from the code, the event handler reads a value of an OPC Item (Demo.Single) and stores it into cell A1 of the worksheet. | As you can guess from the code, the event handler reads a value of an OPC Item (Demo.Single) and stores it into cell A1 of the worksheet. |
Revision as of 15:15, 31 July 2016
This is the simplest Excel VBA example. It reads an OPC item value and stores it in a cell of a worksheet.
Step 1: Create a new Excel worksheet, and select Developer -> Visual Basic from the ribbon:
Step 2. Select Tools -> References, check the box next to "OPC Labs EasyOPC Type Library", and press OK:
Step 3. Select This Workbook in the project tree, and then add a handler for opening the workbook by selecting Workbook and Open from the drop-downs on the top of he code window:
Step 4. Enter the code for the handler as follows:
Private Sub Workbook_Open() ' Create EasyOPC-DA component Dim EasyDAClient As New EasyDAClient ' Read item value and display it Range("A1").Value = EasyDAClient.ReadItemValue("", "OPCLabs.KitServer.2", "Demo.Single") End Sub
As you can guess from the code, the event handler reads a value of an OPC Item (Demo.Single) and stores it into cell A1 of the worksheet.
Step 5. Save the file as "Excel Macro-Enabled Worksheet", close Excel, and then open the file again. The worksheet will read the OPC item and put it into cell A1, so you will see something like this:
This concept can obviously be enhanced in many different ways. Specifically, you may want to tie the OPC actions to different events, such as pressing a button on the sheet, or a periodic timer. This example project is included with the product. Please use the example from the product itself for the most up-to-date code of the example.