Syntax
DDEExecute ChanNum, Command$
Remarks
Sends a command or series of commands to an application through a dynamic-data exchange (DDE) channel.
Argument | Explanation | |
ChanNum | The channel number of the DDE conversation as returned by DDEInitiate(). If ChanNum doesn't correspond to an open channel, an error occurs. | |
Command$ | A command or series of commands recognized by the server application. In Windows, you can also use the format described under SendKeys to send specific key sequences. If the server application can't perform the specified command, an error occurs. |
In Microsoft Excel and many other applications that support DDE, Command$ should be one or more statements or functions in the application's macro language. For example, in Microsoft Excel the XLM macro instruction to create a new worksheet is NEW(1). To send the same command through a DDE channel, you use the following instruction:
DDEExecute channel, "[NEW(1)]"
Note that some applications, including Microsoft Excel, require that each command received through a DDE channel be enclosed in brackets.
You can use a single DDEExecute instruction to send more than one command. For example, the following instruction tells Microsoft Excel to open and then close a worksheet:
DDEExecute channel, "[NEW(1)][FILE.CLOSE(0)]"
Note that there is no space between the bracketed commands; a space character between the commands would cause an error. The preceding instruction is equivalent to the following two instructions:
DDEExecute channel, "[NEW(1)]" DDEExecute channel, "[FILE.CLOSE(0)]"
Many commands require arguments in the form of strings enclosed in quotation marks. Because quotation marks indicate the beginning and end of a string in WordBasic, you must use Chr$(34) to include a quotation mark in a command string. For example, the following instruction tells Microsoft Excel to open SALES.XLS:
DDEExecute channel, "[OPEN(" + Chr$(34) + "SALES.XLS" + Chr$(34) + ")]"
For more information on sending commands to Microsoft Excel and other applications, see Chapter 8, "Communicating with Other Applications" in Part 1, "Learning WordBasic."
Example
This example starts Microsoft Excel if it is not running and then opens a channel to Microsoft Excel and the System topic (for information on the System topic, see DDERequest$( )). The example then sends commands to open SALES.XLS (Windows) or BLUE SKY SALES (Macintosh) and go to cell R4C2. Unlike a document, which may or may not be open, the System topic is always available when Microsoft Excel is running.
Here is the Windows version of the example:
If AppIsRunning("Microsoft Excel") = 0 Then Shell "C:\EXCEL\EXCEL.EXE" channel = DDEInitiate("Excel", "System") q$ = Chr$(34) cmd1$ = "[OPEN(" + q$ + "C:\EXCEL\EXAMPLES\SALES.XLS" + q$ + ")]" cmd2$ = "[SELECT(" + q$ + " R4C2" + q$ + ", " + q$ + " R4C2" + q$ + ")]" bothcmds$ = cmd1$ + cmd2$ DDEExecute channel, bothcmds$
And here is the Macintosh version:
If AppIsRunning(MacID$("XCEL")) = 0 Then Shell MacID$("XCEL") channel = DDEInitiate("Excel", "System") q$ = Chr$(34) cmd1$ = "[OPEN(" + q$ + "HD1:EXCEL:EXAMPLES:BLUE SKY SALES" + q$ + ")]" cmd2$ = "[SELECT(" + q$ + " R4C2" + q$ + ", " + q$ + " R4C2" + q$ + ")]" bothcmds$ = cmd1$ + cmd2$ DDEExecute channel, bothcmds$
See Also
DDEInitiate(), DDEPoke, DDERequest$(), DDETerminate, DDETerminateAll