User Tools

Site Tools


how_to_send_dde_commands_and_data_to_a_dde_server.htm
Navigation:  How To's and Troubleshooting > How to… >====== How to Send DDE Commands and Data to a DDE Server ====== Previous pageReturn to chapter overviewNext page

Once the DDE channel is open, you can use the DDE functions to send commands, data, or requests to the server.

The example code below sends a command to Excel to open a new file and save it under a specified file name. This is a common DDE task when working with commercial applications. Often, the server application allows access to “document” functions only when you specify a document name in the DDECLIENT function. The document name must be a file that already exists.

In this particular case, to execute any “document” actions, such as entering a value in a cell, Excel (and many other applications) require the DDE channel “topic” to be the name of document. Therefore, if your application is providing new data it wants the server to save in a new document file, your application:

·Opens a conversation about the “System” topic.

·Sends a command asking the server to save a document file under a specified name.

·Closes the conversation.

·Opens a second conversation with the server, this time specifying the newly created file's name as the topic.

·Sends the “unsolicited” (because the server didn't ask for it) data and then tells the DDE Server (Excel) to execute commands or other requests for data that apply to the file.

·Closes the conversation.

The following therefore should execute only if the example code shown in the How to Start a DDE Conversation topic was successful.

DDEEXECUTE(Channel,'[NEW(1)]')        ! Excel's File/New command

The DDEEXECUTE statement takes the DDE channel number as its first parameter, and the command string as the second. Excel requires you to enclose all DDE commands in square brackets. This command creates a blank spreadsheet.

The Excel command string enclosed by the square brackets is an Excel macro statement. Excel, and many other applications allow you to send a macro statement via the DDEEXECUTE statement. In this particular case, you don't have to know the name of the open Excel file to execute the statement.

TipBox.jpg

Many commercial applications with their own macro languages allow you to both record and edit macros. Use the application to make a “dry run” of the actions you need it to execute, with its macro recorder turned on. Edit the resulting macro, and use the clipboard to copy each macro statement to your embedded source in the Text Editor. Put each macro statement in the second parameter of the DDEEXECUTE statement, and you can be assured of the correct syntax for the DDE command!

In the next embedded source line, tell Excel to save the new (blank) sheet under a name that you specify.

DDEEXECUTE(Channel, '[SAVE.AS(“DDE_TEST.XLS”,1,“”, FALSE,“”,FALSE)]')

Knowing the name allows you to close this channel, then open another specifying the file name as the topic. Note that the Excel command string requires double-quote marks.

Terminate the channel started under the “System” topic.

DDECLOSE(Channel)              ! Close first DDE channel

Sending Data from Client to Server

To continue the example, to send data to Excel, you need to open another DDE conversation, this time with the newly created file name as the topic:

Open the DDE channel and name the file as the topic.

Channel = DDECLIENT('Excel','DDE_TEST.XLS') !New channel under known filename

To place data in a spreadsheet cell, use the DDEPOKE statement.

DDEPOKE(Channel,'R1C1','999')

Following the successful placement of the value in the spreadsheet, you could then send further Excel macro statements using DDEEXECUTE. This would allow you to send additional spreadsheet data, highlight a range, then tell Excel to draw a chart.

how_to_send_dde_commands_and_data_to_a_dde_server.htm.txt · Last modified: 2021/04/15 15:57 by 127.0.0.1