home *** CD-ROM | disk | FTP | other *** search
- *Note: the non-standard commenting shown here will only work in FoxPro For Dos 2.5 or FoxPro For Windows
-
- #if 0 *------------------------------*
-
- DDE2.PRG
-
- This simple program shows some more DDE basics.
- It demonstrates some more resilient code for
- starting a DDE conversation using DDESetOption,
- and shows the DDEExecute command being used
- with Excel to open a spreadsheet.
-
- *************************************************
-
- When initiating a DDE conversation, DDEInitiate
- will try and start the server program if it is
- not already running. If you wish to suppress the
- resulting message, you can turn it off using the
- DDESetOption function. Then you can use RUN to
- start the program yourself. The following code
- shows how you might do it:
- #endif *------------------------------*
-
- =DDESetOption("SAFETY", .F.)
- channel = DDEInitiate("Excel", "System")
- if channel = -1
- excelexe = locfile("excel.exe", "exe", "Where is excel.exe?")
- if excelexe == ""
- wait window "Could not find excel.exe"
- cancel
- endif
- RUN /N7 &excelexe
- channel = DDEInitiate("Excel", "System")
- if channel = -1
- wait window "DDE Error " + str(DDELastError())+" with Excel"
- cancel
- endif
- endif
-
- #if 0 *------------------------------*
-
- Notice the use of the /N7 option on the RUN
- command. This makes Excel minimized and
- inactive.
-
- Now that Excel is running and we have a
- conversation established, we can send it commands
- with the DDEExecute function. Notice how the
- topic we used was "System". This is the topic
- usually used when you wish to send commands to a
- server.
-
- The exact commands permitted depend totally on
- what the server supports. Even the format of the
- commands is server dependent. Typically,
- however, the server will accept the same commands
- that it allows in the programming language it may
- support. Excel has a built in macro programming
- language, and we can send commands to it by using
- valid macro commands and surrounding them with
- square brackets, '[' and ']'. For example, the
- following will tell Excel to open a given
- spreadsheet. Notice that we use double quotes
- to surround the spreadsheet name. Unlike FoxPro,
- Excel is very particular about which quotes you
- use. Only double quotes will work. This means
- therefore, that you need to use single quotes for
- the strings that you're building.
-
- #endif *------------------------------*
-
- file = sys(2003)+ "\" + "myfile.xls"
- cmd = '[open(' + '"' + file + '")]'
- ? "Cmd = " + cmd
-
- ? DDEExecute(channel, cmd)
-
- #if 0 *------------------------------*
-
- Notice that you need to provide the full path
- to the spreadsheet file. You don't really know
- what directory Excel will have as its working
- directory, so its easier to be sure. Once we
- have the spreadsheet open, we can establish a
- conversation with it and manipulate it in the
- usual way.
-
- #endif *------------------------------*
-
- myfilechan = DDEInitiate("Excel", file)
- ? DDERequest(myfilechan, "R1C1")
- ? DDETerminate(myfilechan)
-
- #if 0 *------------------------------*
-
- To tidy up, we can now send a command to Excel
- to tell it to close itself. We could also close
- individual files. For exact details, see the
- Excel macro language manuals
-
- #endif *------------------------------*
-
- ? DDEExecute(channel, "[File.Close()]")
- ? DDEExecute(channel, "[Quit()]")
-
- #if 0 *------------------------------*
-
- Note that if we now try and close the channel
- with DDETerminate, we'll get an error. That is
- ok, because we actually broke the channel when
- we told Excel to close. DDELastError() confirms
- that that is the problem.
-
- #endif *------------------------------*
-
- ? DDETerminate(channel)
- ? "Error was: ", DDELastError()
-