Overview of Using DDE with Word for WindowsLast reviewed: July 30, 1997Article ID: Q96946 |
The information in this article applies to:
SUMMARYDynamic data exchange (DDE) is a built-in feature of Microsoft Windows that allows two Windows applications to share data and send commands back and forth. A successful DDE process does the following:
DDEInitiate Opens a DDE channel from Word to another application ----------- such as Microsoft Excel. A channel must be opened before and other DDE commands can be executed. The syntax for the DDEInitiate command is ChanNum = DDEInitiate (App$, Topic$) where ChanNum is a variable that contains the unique identification of the DDE channel, App$ is a string variable identifying the name of the application with which the link is established, and Topic$ is a string variable identifying a specific object in App$ (for example, REVENUE.XLS in Microsoft Excel). A special topic called "system" is also available for most DDE aware applications. The system topic can be used to communicate in general with App$ when no specific data file is desired. DDERequest Requests information from the connected application. ---------- Information must be returned in text string format and is immediately assigned to a text variable. The syntax of the DDEExecute command is Result$ = DDERequest$ (ChanNum, Item$) where Result$ is the string variable that will hold the returned information, ChanNum is a variable containing the unique DDE channel identification to a specific application as assigned through DDEInitiate, and Item$ is a string variable containing the location of the information in that application ("R1C1" in the application Microsoft Excel for example). DDEPoke Sends information to the connected application. The ------- syntax of the DDEPoke command is DDEPoke ChanNum, Item$, Data$ where ChanNum is a variable containing the unique DDE channel identification to a specific application as assigned through DDEInitiate, Item$ defines the destination location in that application (such as "R1C1" in the application Microsoft Excel), and Data$ is the actual information to transfer. DDEExecute Runs a command in the connected application. The ---------- syntax for the DDEExecute command is DDEExecute ChanNum, ExecuteString$ where ChanNum is a variable containing the unique DDE channel identification to a specific application as assigned through DDEInitiate, and ExecuteString$ is a specific command recognized by that application. DDETerminate Ends the conversation with the connected application. ------------ The syntax for the DDETerminate command is DDETerminate ChanNum where ChanNum is a variable containing the unique DDE channel identification to a specific application as assigned through DDEInitiate. A variation of this command available in Word for Windows is DDETerminateAll. This command, which has no arguments, will end conversations with all connected applications. This command is specific to Word for Windows and a similar command may not be available in other DDE aware applications. The syntax for the DDETerminateAll command is: DDETerminateAll To Use DDE to Connect Word for Windows and Microsoft ExcelThe following example prompts for a number of days and then uses Microsoft Excel to add this number to today's date. The DDE conversation is initiated with the spreadsheet named "Sheet1". Today's date is sent to Microsoft Excel using the DDEPoke command. The DDEExecute command is use to select and apply the "m/d/yy" format to the R1C2 cell. The result of today's date plus the specified amount of days is returned to Word for Windows with the DDERequest$() command.
Declare Function IsAppLoaded Lib "kernel"(name$) As Integer Alias "GetModuleHandle" Sub Main days$ = InputBox$("How many days beyond today's date?") If IsAppLoaded("EXCEL") = 0 Then Shell "Excel" ChanNum = DDEInitiate("Excel", "Sheet1") DDEPoke ChanNum, "R1C1", Date$() 'Put today's date in R1C1 '=== Calculate today's date + the number of days in R1C2 DDEPoke ChanNum, "R1C2", "=A1+" + days$ DDEExecute ChanNum, "[SELECT(" + Chr$(34) + "r1c2" + Chr$(34) + ")]" DDEExecute ChanNum, "[FORMAT.NUMBER(" + Chr$(34) + \ "m/d/yy" + Chr$(34) + ")]" a$ = DDERequest$(ChanNum, "R1C2") MsgBox a$, "The date is " + days$ + " days away!" DDETerminate ChanNum End SubReference(s): "Using WordBasic" by WexTech Systems and Microsoft
|
KBCategory: kbmacro
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |