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

  1. *                DDEWORD2.PRG
  2. * This simple program shows some more DDE basics.
  3. * It demonstrates some more resilient code for
  4. * starting a DDE conversation using DDESetOption, 
  5. * and shows the DDEExecute command being used 
  6. * with Word to open a file and insert text.
  7. * *************************************************
  8. * When initiating a DDE conversation, DDEInitiate
  9. * will try and start the server program if it is
  10. * not already running.  If you wish to suppress the
  11. * resulting message, you can turn it off using the
  12. * DDESetOption function.  Then you can use RUN to
  13. * start the program yourself.  The following code 
  14. * shows how you might do it:
  15.  
  16. = DDESetOption("SAFETY", .F.)
  17. channel = DDEInitiate("WinWord", "System")
  18. if channel = -1
  19.   exe = locfile("winword.exe", "exe", "Where is winword.exe?")
  20.   if exe == ""
  21.     wait window "Could not find winword.exe"
  22.     cancel
  23.   endif
  24.   RUN /N7 &exe
  25.   channel = DDEInitiate("WinWord", "System")
  26.   if channel = -1
  27.     wait window "DDE Error " + str(DDELastError())+" with Word"
  28.     cancel
  29.   endif
  30. endif
  31.  
  32. * Notice the use of the /N7 option on the RUN 
  33. * command.  This makes Word minimized and 
  34. * inactive.
  35. * Now that Word is running and we have a 
  36. * conversation established, we can send it commands
  37. * with the DDEExecute function.  Notice how the
  38. * topic we used was "System".  This is the topic
  39. * usually used when you wish to send commands to a 
  40. * server.
  41. * The exact commands permitted depend totally on
  42. * what the server supports.  Even the format of the
  43. * commands is server dependent.  Typically, 
  44. * however, the server will accept the same commands
  45. * that it allows in the programming language it may
  46. * support.  Word has a built in macro programming
  47. * language, and we can send commands to it by using
  48. * valid macro commands and surrounding them with 
  49. * square brackets, '[' and ']'.  For example, the
  50. * following will tell Word to open a given
  51. * file.  Notice that we use double quotes
  52. * to surround the file name.  Unlike FoxPro,
  53. * Word is very particular about which quotes you
  54. * use.  Only double quotes will work.  This means
  55. * therefore, that you need to use single quotes for
  56. * the strings that you're building.
  57.  
  58. file = sys(2003)+ "\" + "myfile.doc"
  59. cmd = '[FileOpen .name=' + '"' + file + '"]'
  60. ? "Cmd = " + cmd
  61.  
  62. ? DDEExecute(channel, cmd)
  63.  
  64. * Notice that you need to provide the full path
  65. * to the Word file.  You don't really know
  66. * what directory Word will have as its working
  67. * directory, so its easier to be sure.  Once we 
  68. * have the Word file open, we can establish a 
  69. * conversation with it.  After opening the channel
  70. * the following code will insert a bookmark, a 
  71. * field for the current date, and then some text.
  72. * (See the Word macro language online help for the
  73. * gory details of WordBasic)
  74.  
  75. myfilechan = DDEInitiate("WinWord", file)
  76. ? DDEExecute(myfilechan, '[insertbookmark .name="test"]')
  77. ? DDEExecute(myfilechan, '[insertfield .field="createdate \@ mm/dd/yy"]')
  78. ? DDEPoke(myfilechan, "test", "This is some text inserted on ")
  79. ? DDEExecute(myfilechan, "[insertpara]")
  80. WAIT WINDOW "Switch to Word to see the results..."
  81. ? DDEExecute(myfilechan, "[fileclose 2]")
  82.  
  83. ? DDETerminate(myfilechan)
  84.  
  85. * To tidy up, we can now send a command to Word
  86. * to tell it to close itself.  We could also close
  87. * individual files.  For exact details, see the
  88. * Word macro language manual.
  89.  
  90. ? DDEExecute(channel, "[FileExit]")
  91.  
  92. * Note that if we now try and close the channel
  93. * with DDETerminate, we'll get an error.  That is
  94. * ok, because we actually broke the channel when
  95. * we told Word to close.  DDELastError() confirms
  96. * that that is the problem.
  97.  
  98. ? DDETerminate(channel)
  99. ? "Error was: ", DDELastError()