home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / CLIPPER / DATABASE / FOXDDE.ZIP / DDEXL1.PRG < prev   
Encoding:
Text File  |  1993-04-16  |  3.7 KB  |  108 lines

  1. * DDEXL1.PRG
  2. *
  3. * This simple program shows the basic steps 
  4. * needed for FoxPro to access another application
  5. * acting as a DDE server.  To see how it works, 
  6. * single step through the code, reading the comments
  7. * as you go.  It shows the use of DDEInitiate,
  8. * DDERequest, DDEPoke, DDETerminate and DDELastError.
  9.  
  10. ****************************************************
  11.  
  12. * The first thing we have to do is start a 
  13. * conversation with the server.  In this case,
  14. * we're going to "talk" to Microsoft Excel.
  15.  
  16. * To do this we start or "initiate" the conversation
  17. * by opening up a "channel" to Excel.  (Note, if 
  18. * Excel doesn't start on the next step, then either
  19. * start it manually and retry, or place it in your
  20. * MS-DOS path before starting Windows.)
  21.  
  22. channel = DDEInitiate("Excel", "Sheet1")
  23. ? "Channel is:", channel
  24.  
  25. * This command will try and start Excel if it isn't
  26. * already running and then tell it that it wants to
  27. * talk about "Sheet1".  This is called the "topic" 
  28. * of the conversation.  Every conversaction is 
  29. * about only one topic.  Only certain topics are
  30. * understood by the server and only the server
  31. * documentation can tell you what those are.  In 
  32. * the case of Excel, we are asking to talk about
  33. * a spreadsheet called "Sheet1" -- the default 
  34. * empty spreadsheet that Excel opens.  
  35.  
  36. * Let's ask Excel what is in cell R1C1 of the Sheet1.
  37. * We do this with the DDERequest command, like so:
  38.  
  39. ? "<" + DDERequest(channel, "R1C1") + ">"
  40.  
  41. * Notice how the cell is empty.  If we switch over
  42. * to Excel and put something in the cell, we can
  43. * try again.
  44.  
  45. ? "<" + DDERequest(channel, "R1C1") + ">"
  46.  
  47. * This time we get the value of the cell.  Notice 
  48. * how this is returned as a string.  Everything
  49. * returned by a simple DDERequest like this will be
  50. * a string, so if you're expecting numbers, you'll
  51. * have to convert them into numbers explicitly.
  52.  
  53. * In the simple DDERequests above, the second 
  54. * parameter passed to the function is called the
  55. * "Item".  The Item defines what this part of the
  56. * conversation is about.  Remember that we already
  57. * know that the main topic of conversation is
  58. * "Sheet1".  The Item is a sub-topic within
  59. * the main topic.
  60.  
  61. * Another DDE command that Excel understands in a
  62. * conversation about a spreadsheet is the "Poke" 
  63. * command.  The DDEPoke command below tells Excel
  64. * "Here, take this value and do something with it".
  65. * We provide an Item to help clarify exactly what
  66. * to do with it.  Thus:
  67.  
  68. ? DDEPoke(channel, "R1C2", "2")
  69.  
  70. * will place the value 2 into cell R1C2.  The "R1C1"
  71. * parameter is still called the "Item".  The "2"
  72. * parameter is called the "Data".  Note that it is 
  73. * string value.  Just as DDERequest only returns
  74. * strings, DDEPoke only sends strings.  
  75.  
  76. * We can also 'poke' a formula into a cell, e.g.
  77.  
  78. ? DDEPoke(channel, "R1C3", "=A1+B1")
  79.  
  80. * (Note that the Topic always uses RC notation, but
  81. * the Data uses whatever mode Excel is currently in.)
  82. * Switching to Excel shows that this works.  In 
  83. * fact, we can now retrieve the summed cells from
  84. * Excel using:
  85.  
  86. ? "<" + DDERequest(channel, "R1C3") + ">"
  87.  
  88. * When we're finished with a conversation, we can
  89. * end it by using the "DDETerminate" function.  I.e.
  90.  
  91. ? DDETerminate(channel)
  92.  
  93. * Notice how all the DDE commands are actually 
  94. * functions.  I.e., they all return a value.  
  95. * Typically this value will be .T.  If an error
  96. * occurs, then .F. will be returned.  You can then
  97. * use the DDELastError function to determine why it
  98. * failed.  For example, if I now try and Poke to
  99. * the channel I just closed:
  100.  
  101. ? DDEPoke(channel, "R1C3", "=A1+B1")
  102.  
  103. * I get .F. returned.  By checking DDELastError
  104.  
  105. ? DDELastError()
  106.  
  107. * and looking up the help entry for it, you can see
  108. * what went wrong.