<< Click to Display Table of Contents >> Manual > Server Creation Guide > Interface > DDE Interface > DDE connection from ExcelVBA > Step 3: Reading and writing values |
Step 3: Reading and writing values
Import
1.Write the following definition in "Module1".
Sub Test1() Dim chan As Variant Dim result As Variant chan = DDEInitiate("FASERVER", "U01.F01") result = DDERequest(chan, "T01") Range("B1").Value = result(1) DDETerminate (chan) End Sub
With this statement, a procedure called "Test1" has been defined. This procedure can now be called from a button placed on an Excel sheet.
Details of each command are as follows:
command |
explanation |
DDEInitiate
|
This is a command to start DDE communication. Specify the server application name and topic name. If successful, it returns a DDE communication channel. Use this channel to execute other DDE communication related commands. |
DDERequest
|
This command requests data from the server. Specify the device for which data is requested in the item name. If successful, the requested data is returned in an array. |
DDETerminate
|
This is a command to end DDE communication. |
2.Place a Read button so that you can load data at any time.
To place the
button, go to Insert in the Developer tab.
Here, place the buttons as follows:
3.Right-click the button and select Assign Macro.
4.Select the "Test1" procedure written in step 1 and register the macro.
With this setting, the "Test1" process will be called when the button is pressed.
write
1.Write the following definition in "Module1".
Sub Test2() Dim chan As Variant Dim result As Variant chan = DDEInitiate("FASERVER", "U01.F01") DDEPoke chan, "T01", Range("A1") DDETerminate (chan) End Sub
This description defines a procedure called "Test2".
The command details are as follows:
command |
explanation |
DDEPoke |
This is a command to write data to the server. Specify the device to which you want to write data in the item name. |
2.To be able to immediately check the results, enter the following DDE formula in any cell other than "A1".
=FASERVER|U01.F01!T01 |
With the above, the value of the tag "U01.F01.T01" will be displayed in the target cell.
Furthermore, move the caret (text input cursor) anywhere between "Sub Test2()" and "End Sub" and select "Run Sub/ User Form" from the "Run" menu.
3.Write the data.
Enter the value you want to write to the tag in "A1", move the caret (text input cursor) somewhere between "Sub Test2()" and "End Sub", and select "Run Sub/ User Form" from the "Run" menu.
When you run
VBA, you will see that the value of the cell containing the DDE formula is updated with the value entered in A1.
In addition, the above process can also be executed by calling "Test2" from the button in the same way as the Read button.
By using arrays, a single tag can handle large amounts of data as a block. This is particularly fast when dealing with many devices, compared to calling DDERequest or DDEPoke one at a time. However, please note that when sending and receiving arrays with Excel, there may be a limit to the array size. This size limit may change depending on the version of Excel, so if you need to know the limit, please check with Microsoft.
In addition, we have prepared a sample using arrays in the sample download for "DDE connection from ExcelVBA". Please refer to it as necessary. |