Merging Superbase Data into Microsoft Word via DDEPoke Summary: This technote illustrates an alternative method to placing data into Microsoft Word for Windows documents from Superbase. Unlike the prior methods of placing a paste linked field onto a document, this method relies on using Word bookmarks and Word reference fields to place data into a Word document. This method provides greater control over what is being sent to the Word document. This example is also a very good example of using DDEPoke in a Superbase application. To completely understand this technote, you must be aware that Dynamic Data Exchange often requires you to know the macro languages of both applications being used in the DDE link. Examples of this are the Word Basic commands being executed with the DDEExec command in Superbase. It's a good idea to have knowledge of both applications. I highly recommend getting the free WordBasic technical manual from Microsoft for this reason. Creating the Word document The most confusing step in this method of merging data with documents takes place on the Word side of the DDE link. You use a combination of bookmarks and cross-references in a rather unorthodox but legitimate manner. To understand why these two features are needed, just picture using variables in place of your data on the document. To carry out this analogy, I recommend placing a $ before and after the destination field. This gives you a point of reference whenever you wish to view or change the document. For example, the field, lastname, would be reference on the Word document with the variable, $lastname$. You do not have to necessarily reference the field with its exact fieldname, but I recommend it because it makes the document much easier to understand whenever you want to change it. If you want to use something other than a field in your document, you can use any identifier you wish. However, I recommend this identifier has at least some semblance of what you want to print out on the document i.e. $firstinit$ for something like left$(firstname,1). Create your document as you normally would but wherever you want to place data, place an identifier surrounded by $ in place of the actual data. For example, "Dear John Smith," becomes "Dear $Firstname$ $Lastname$,". The next step is to add bookmarks to your document. a) Find the first or only instance of an identifier. b) Highlight the identifier, including the $s, if you used them. c) Select Bookmark from the Insert menu. d) For the bookmark name, enter the name of the identifier, excluding the $s, if you used them. e) Click OK to make that identifier the current bookmark for that object. Repeat steps a through e until all first or only instances of the identifiers are bookmarked. After bookmarking each identifier, go back to any non-bookmarked instances of the identifiers and place references on them. a) Highlight the identifier. b) Select Field from the Insert menu. c) Choose Reference from the Insert Field Type box. d) Choose the identifier's bookmark from the Instructions box. e) Click OK to continue. Repeat steps a through e until all non-bookmarked identifiers are referenced. Make sure that the Field Codes selection of the View menu is not checked. Having it checked shows the reference fields as {reference.....} instead of any data. Making sure this is not checked allows the document to print out data instead of the Word code. Save your document from the File menu and minimize Word. It might be useful to print out the document as it is now so that you can have a reference of all the identifiers for the following application in Superbase. Superbase application for merging data with Word On the Superbase end of the merge, there are also many changes needed to run the merge. One of the major departures from previous method is the ability to place data directly onto a document in a more controlled fashion. In other words, you are not limited to sending just fields. For instance, calculations and aggregate functions can be sent over to Word from reports using this method. This method also allows for the ability to check data before it is sent to Word. For instance, you can check to see if a field has data or not and send over alternative data if desired. The following code sends data to a Word document from a database called "test". Notice that the DDEPoke command's first string parameter or location is the name of a bookmark in your Word document. For instance, to place the field, lastname.test, into a bookmark called "lastname", you need a line like the following: DDEPoke 1, "lastname", lastname.test The number is the DDE channel number which can be any number from 0 to 32767 and is opened by the DDEInit command. Notice some non-Superbase commands in the DDEExec lines. These commands are WordBasic commands and can be referenced in the WordBasic manual provided by Microsoft. The following code segment illustrates how to merge a record into a Word document and print the document: 10 REM DDETERM makes sure all DDE channels are closed. 11 DDETERM 5 12 DDETERM 6 13 ch$ = CHR$ (34) 14 REM Open MS Word. Checks to see if MS Word is already open. 15 REM If so, continues on. If not, then Superbase opens MS Word. 16 REM This section uses a Windows DLL call to perform this function. 17 REGISTER CLEAR 18 REGISTER "Kernel","GetModuleHandle","IC" 19 modname$ = "MSWORD" 20 hMod% = CALL ("GetModuleHandle",modname$) 21 REM Change the path to reflect your actual MS Word directory if necessary. 22 IF (hMod% = 0) THEN CALL "c:\winword\winword.exe",2 23 REGISTER CLEAR 24 REM Open DDE channel to the System Topic. Useful for non-document commands. 25 DDEINIT 5,"WinWord","system" 26 REM Place the path and name of your document inside the "". 27 wordfile$ = "c:\sbstuff\onemany\letter.doc" 28 REM Open your document and initialize the channel to that document. 29 DDEEXEC 5,"[FileOpen.name = " + ch$ + wordfile$ + ch$ + "]" 30 DDEINIT 6,"WinWord",wordfile$ 31 REM Here is where you begin placing data into the bookmarks. 32 REM Notice the LEFT$(firstname.letter,1) is a calculation for the first initial of the first- name. 33 DDEPOKE 6,"lastname",lastname.letter 34 DDEPOKE 6,"firstname",firstname.letter 35 DDEPOKE 6,"firstinit", LEFT$ (firstname.letter,1) 36 DDEPOKE 6,"city",city.letter 37 REM Note: Long text fields with returns will keep their returns in the Word document. 38 DDEPOKE 6,"longtext",longtext.letter 39 REM EditSelectAll selects all the Word document to update it. 40 DDEEXEC 6, "[EditSelectAll]" 41 REM UpdateFields updates all the bookmarks and reference fields. 42 DDEEXEC 6, "[UpdateFields]" 43 REM StartOfDocument de-selects document and places cursor at start of document. 44 DDEEXEC 6, "[StartOfDocument]" 45 REM FilePrint will print out the document to the printer 46 DDEEXEC 6, "[FilePrint]" 47 REM FileClose closes the document. The 2 tells Word not the save file with data. 48 REM Normally, you wouldn't want to save the data in the document so 2 prevents this. 49 DDEEXEC 6,"[FileClose 2]" Note: the line numbers should not be entered. They are there for reference only. To make the above code segment work with your database, do the following: 1. Make sure that line 22 correctly reflects the path to your Microsoft Word directory. 2. Modify line 27 to reflect the name and path of your document. 3. Lines 33 to 38 are where you place the commands to send your data to Word. The format of DDEPoke is as follows: DDEPoke channel, bookmark, data Where Represents channel Current DDE channel to the Word document. bookmark Bookmark in Word document i.e. "LastName" data Superbase data that you want at the bookmark. This can be a fieldname, calculation, variable, and so on. You are not limited to the amount of data you would like to place in the Word document via this method. The only possible limit is the number of bookmarks available for a Word document which is 450. 4. When you want to close Word, add the following line to your code: DDEEXEC 5,"[FileExit]" This method of placing bookmarks on Word documents makes the merging process more flexible and adds greater flexibility to DDE itself. One interesting use for this method would be to use external files from Superbase in your Word document. This will be documented in a later technote. One important note about using DDE with any application: Whenever you receive an "Application Not Responding" error after doing a DDEExec, DDEPoke, or DDEReq, the command string you are sending is not being recognized by the other application. In other words, the application cannot understand what you are requesting from that application. Usually, this is just an error in syntax which can be quickly resolved by checking on the command string if you stored it in a variable. If stored in a variable, check it by printing out the memory and checking to see if doing the same command string would work correctly if done manually in the other application. The most common problem is that you must make sure that all required quotes are in the right places when you send the command string to the other application. You can manually add the quotes by adding a CHR$(34) to the command string. Program: Superbase Versions: 2.0 Date: July 6, 1993 D Date: