Overview of Using DDE with Word for Windows

Last reviewed: July 30, 1997
Article ID: Q96946
The information in this article applies to:
  • Microsoft Word for Windows, versions 1.0, 1.10, 1.10a, 2.0, 2.0a,

        2.0a-CD, 2.0b, 2.0c
    

SUMMARY

Dynamic 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:

  • Opens a communication channel between applications
  • Requests information from another application
  • Gives information to another application
  • Performs actions in another application
  • Closes the communication channel

In Word for Windows, these basic processes are implemented through the following WordBasic commands (the example at the end of this document shows how you can use these DDE commands to communicate between Word for Windows and Microsoft Excel for Windows):

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 Excel

The 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 Sub

Reference(s):

"Using WordBasic" by WexTech Systems and Microsoft


KBCategory: kbmacro
KBSubcategory:
Additional query words: 1.0 1.10 1.10a 2.0 2.0a 2.0b 2.0c
winword2 winword dynamic data exchange DDEPoke DDETerminateAll
DDERequest DDETerminate DDEExecute DDEInitiate


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: July 30, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.