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

  1. *Note: the non-standard commenting shown here will only work in FoxPro For Dos 2.5 or FoxPro For Windows
  2.  
  3. #if 0    *------------------------------*
  4.  
  5.                DDE2.PRG
  6.  
  7. This simple program shows some more DDE basics.
  8. It demonstrates some more resilient code for
  9. starting a DDE conversation using DDESetOption, 
  10. and shows the DDEExecute command being used 
  11. with Excel to open a spreadsheet.
  12.  
  13. *************************************************
  14.  
  15. When initiating a DDE conversation, DDEInitiate
  16. will try and start the server program if it is
  17. not already running.  If you wish to suppress the
  18. resulting message, you can turn it off using the
  19. DDESetOption function.  Then you can use RUN to
  20. start the program yourself.  The following code 
  21. shows how you might do it:
  22. #endif    *------------------------------*
  23.  
  24. =DDESetOption("SAFETY", .F.)
  25. channel = DDEInitiate("Excel", "System")
  26. if channel = -1
  27.   excelexe = locfile("excel.exe", "exe", "Where is excel.exe?")
  28.   if excelexe == ""
  29.     wait window "Could not find excel.exe"
  30.     cancel
  31.   endif
  32.   RUN /N7 &excelexe
  33.   channel = DDEInitiate("Excel", "System")
  34.   if channel = -1
  35.     wait window "DDE Error " + str(DDELastError())+" with Excel"
  36.     cancel
  37.   endif
  38. endif
  39.  
  40. #if 0    *------------------------------*
  41.  
  42. Notice the use of the /N7 option on the RUN 
  43. command.  This makes Excel minimized and 
  44. inactive.
  45.  
  46. Now that Excel is running and we have a 
  47. conversation established, we can send it commands
  48. with the DDEExecute function.  Notice how the
  49. topic we used was "System".  This is the topic
  50. usually used when you wish to send commands to a 
  51. server.
  52.  
  53. The exact commands permitted depend totally on
  54. what the server supports.  Even the format of the
  55. commands is server dependent.  Typically, 
  56. however, the server will accept the same commands
  57. that it allows in the programming language it may
  58. support.  Excel has a built in macro programming
  59. language, and we can send commands to it by using
  60. valid macro commands and surrounding them with 
  61. square brackets, '[' and ']'.  For example, the
  62. following will tell Excel to open a given
  63. spreadsheet.  Notice that we use double quotes
  64. to surround the spreadsheet name.  Unlike FoxPro,
  65. Excel is very particular about which quotes you
  66. use.  Only double quotes will work.  This means
  67. therefore, that you need to use single quotes for
  68. the strings that you're building.
  69.  
  70. #endif    *------------------------------*
  71.  
  72. file = sys(2003)+ "\" + "myfile.xls"
  73. cmd = '[open(' + '"' + file + '")]'
  74. ? "Cmd = " + cmd
  75.  
  76. ? DDEExecute(channel, cmd)
  77.  
  78. #if 0    *------------------------------*
  79.  
  80. Notice that you need to provide the full path
  81. to the spreadsheet file.  You don't really know
  82. what directory Excel will have as its working
  83. directory, so its easier to be sure.  Once we 
  84. have the spreadsheet open, we can establish a 
  85. conversation with it and manipulate it in the 
  86. usual way.
  87.  
  88. #endif    *------------------------------*
  89.  
  90. myfilechan = DDEInitiate("Excel", file)
  91. ? DDERequest(myfilechan, "R1C1")
  92. ? DDETerminate(myfilechan)
  93.  
  94. #if 0    *------------------------------*
  95.  
  96. To tidy up, we can now send a command to Excel
  97. to tell it to close itself.  We could also close
  98. individual files.  For exact details, see the
  99. Excel macro language manuals 
  100.  
  101. #endif    *------------------------------*
  102.  
  103. ? DDEExecute(channel, "[File.Close()]")
  104. ? DDEExecute(channel, "[Quit()]")
  105.  
  106. #if 0    *------------------------------*
  107.  
  108. Note that if we now try and close the channel
  109. with DDETerminate, we'll get an error.  That is
  110. ok, because we actually broke the channel when
  111. we told Excel to close.  DDELastError() confirms
  112. that that is the problem.
  113.  
  114. #endif    *------------------------------*
  115.  
  116. ? DDETerminate(channel)
  117. ? "Error was: ", DDELastError()
  118.