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

  1. * DDEWORD1.PRG
  2. *
  3. * This simple program shows the basic steps 
  4. * needed for FoxPro to access Microsoft's Word 
  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 Word.
  15.  
  16. * To do this we start or "initiate" the conversation
  17. * by opening up a "channel" to Word.  (Note, if 
  18. * Word 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("WinWord", "System")
  23. ? "Channel is:", channel
  24.  
  25. * This command will try and start Word if it isn't
  26. * already running and then tell it that it wants to
  27. * talk about "System".  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 Word, we are asking to talk about
  33. * a special topic called "System", which most 
  34. * dde servers understand.
  35.  
  36. * Let's ask Word what Topics are available.  
  37. * We do this with the DDERequest command, like so:
  38.  
  39. ? "<" + DDERequest(channel, "Topics") + ">"
  40.  
  41. * Notice how the list includes the name "Document1".
  42. * If other files are open they'll be listed there too.
  43. * If we switch over to Word and open another document, 
  44. * we can try again.
  45.  
  46. ? "<" + DDERequest(channel, "Topics") + ">"
  47.  
  48. * This time we get the newly opened document as well.
  49. * Notice how this is returned as a string with tabs
  50. * delimiting each "thing".  Everything returned by a 
  51. * simple DDERequest like this will be a string.  Tabs
  52. * are used to delimit things within a row, and CR LF
  53. * pairs mark the end of each row (if there's more than
  54. * one).
  55.  
  56. * In the simple DDERequests above, the second 
  57. * parameter passed to the function is called the
  58. * "Item".  The Item defines what this part of the
  59. * conversation is about.  Remember that we already
  60. * know that the main topic of conversation is
  61. * "System".  The Item is a sub-topic within
  62. * the main topic.
  63.  
  64. * If we open a conversation on the document itself,
  65. * rather than "System", we can find out what is in
  66. * the document.  This is done by retrieving the contents
  67. * of a special pre-defined Word bookmark called "\doc".
  68.  
  69. chan2 = DDEInitiate("WinWord", "Document1")
  70. ? "<" + DDERequest(chan2, "\doc") + ">"
  71.  
  72. * If we switch over to Word and put some text in there
  73. * we can verify that it is retrieved via DDE.
  74.  
  75. wait window "Switch to Word and enter some text..."
  76. ? "<" + DDERequest(chan2, "\doc") + ">"
  77.  
  78. * When we're finished with a conversation, we can
  79. * end it by using the "DDETerminate" function.  I.e.
  80.  
  81. ? DDETerminate(chan2)
  82. ? DDETerminate(channel)
  83.  
  84. * Notice how all the DDE commands are actually 
  85. * functions.  I.e., they all return a value.  
  86. * Typically this value will be .T.  If an error
  87. * occurs, then .F. will be returned.  You can then
  88. * use the DDELastError function to determine why it
  89. * failed.  For example, if I now try and Poke to
  90. * the channel I just closed:
  91.  
  92. ? "<" + DDERequest(channel, "Topics") + ">"
  93.  
  94. * I get nothing returned.  By checking DDELastError
  95.  
  96. ? DDELastError()
  97.  
  98. * and looking up the help entry for it, you can see
  99. * what went wrong (bad channel number).
  100.