Difference between revisions of "How to read an OPC-UA node value in Excel"

From OPC Labs Knowledge Base
Jump to navigation Jump to search
Line 12: Line 12:
 
Step 4. Enter the code for the handler as follows:
 
Step 4. Enter the code for the handler as follows:
  
     ' Create EasyOPC-UA component
+
     <span style='color:#696969; '>' Create EasyOPC-UA component</span>
     Dim Client As New EasyUAClient
+
     <span style='color:#800000; font-weight:bold; '>Dim</span> Client <span style='color:#800000; font-weight:bold; '>As</span> <span style='color:#800000; font-weight:bold; '>New</span> EasyUAClient
 
      
 
      
     ' Read node value and display it
+
     <span style='color:#696969; '>' Read node value and display it</span>
     Range("A1").Value =   
+
     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>    
         Client.ReadValue("http://localhost:51211/UA/SampleServer",  
+
         Client<span style='color:#008c00; '>.</span>ReadValue<span style='color:#808030; '>(</span><span style='color:#0000e6; '>"http://localhost:51211/UA/SampleServer"</span><span style='color:#808030; '>,</span>
         "nsu=http://test.org/UA/Data/;i=10853")
+
         <span style='color:#0000e6; '>"nsu=http://test.org/UA/Data/;i=10853"</span><span style='color:#808030; '>)</span>
  
 
As you can guess from the code, the event handler reads a value of an OPC-UA node 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-UA node and stores it into cell A1 of the worksheet.

Revision as of 15:24, 31 July 2016

This is the simplest Excel VBA example. It reads an OPC-UA node 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:

Howtoreadanopcuanodevalueinexcel-1.png

Step 2. In “Microsoft Visual Basic for Applications” window, select Tools -> References, check the box next to "OPC Labs EasyOPC-UA Type Library", and press OK:

Howtoreadanopcuanodevalueinexcel-2.png

Step 3. Select and double-click 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 the code window:

Howtoreadanopcuanodevalueinexcel-3.png

Step 4. Enter the code for the handler as follows:

   ' Create EasyOPC-UA component
   Dim Client As New EasyUAClient
   
   ' Read node value and display it
   Range("A1").Value =   
       Client.ReadValue("http://localhost:51211/UA/SampleServer", 
       "nsu=http://test.org/UA/Data/;i=10853")

As you can guess from the code, the event handler reads a value of an OPC-UA node 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:

Howtoreadanopcuanodevalueinexcel-4.png



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.