home *** CD-ROM | disk | FTP | other *** search
- * DDEWORD2.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 Word to open a file and insert text.
- *
- * *************************************************
- *
- * 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:
-
- = DDESetOption("SAFETY", .F.)
- channel = DDEInitiate("WinWord", "System")
- if channel = -1
- exe = locfile("winword.exe", "exe", "Where is winword.exe?")
- if exe == ""
- wait window "Could not find winword.exe"
- cancel
- endif
- RUN /N7 &exe
- channel = DDEInitiate("WinWord", "System")
- if channel = -1
- wait window "DDE Error " + str(DDELastError())+" with Word"
- cancel
- endif
- endif
-
- * Notice the use of the /N7 option on the RUN
- * command. This makes Word minimized and
- * inactive.
- *
- * Now that Word 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. Word 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 Word to open a given
- * file. Notice that we use double quotes
- * to surround the file name. Unlike FoxPro,
- * Word 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.
-
- file = sys(2003)+ "\" + "myfile.doc"
- cmd = '[FileOpen .name=' + '"' + file + '"]'
- ? "Cmd = " + cmd
-
- ? DDEExecute(channel, cmd)
-
- * Notice that you need to provide the full path
- * to the Word file. You don't really know
- * what directory Word will have as its working
- * directory, so its easier to be sure. Once we
- * have the Word file open, we can establish a
- * conversation with it. After opening the channel
- * the following code will insert a bookmark, a
- * field for the current date, and then some text.
- * (See the Word macro language online help for the
- * gory details of WordBasic)
-
- myfilechan = DDEInitiate("WinWord", file)
- ? DDEExecute(myfilechan, '[insertbookmark .name="test"]')
- ? DDEExecute(myfilechan, '[insertfield .field="createdate \@ mm/dd/yy"]')
- ? DDEPoke(myfilechan, "test", "This is some text inserted on ")
- ? DDEExecute(myfilechan, "[insertpara]")
- WAIT WINDOW "Switch to Word to see the results..."
- ? DDEExecute(myfilechan, "[fileclose 2]")
-
- ? DDETerminate(myfilechan)
-
- * To tidy up, we can now send a command to Word
- * to tell it to close itself. We could also close
- * individual files. For exact details, see the
- * Word macro language manual.
-
- ? DDEExecute(channel, "[FileExit]")
-
- * 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 Word to close. DDELastError() confirms
- * that that is the problem.
-
- ? DDETerminate(channel)
- ? "Error was: ", DDELastError()