Home
SEARCH

 

DDE Examples for Word


Reading Serial Data into a Word Document Using DDE
(This example is for all 32 bit Versions of Word including Word 95, 97 and 2000)

  Steps for setting up WinWedge

1. Select "DDE Server" from the MODE menu and enter: WinWord as the Application Name and System as the DDE topic in the dialog box that appears and click the OK button.

2. Select "Input Data Record Structure" from the DEFINE menu and define the structure of the input records to the Wedge. When you get to the final Window entitled "Input Record Definition Editor", enter the string: [GRABDATA()] as the "Field Postamble DDE Command" after the last data field that you have defined. This is a DDE command that will be sent to Word after each data record is received by the Wedge.

3. Set up the rest of WinWedge parameters and activate it.

  Steps for setting up Word

When you create a VBA macro or subroutine in Word and save it in the Global macro sheet, the name of the subroutine automatically becomes a valid Word DDE command. Create a new macro by selecting "Macro" from the TOOLS menu. When the dialog box appears prompting for a macro name, enter the name: GRABDATA and then click the button labeled "Create". Enter the following code in the macro editor window:

 

Sub GrabData()

chan = DDEInitiate("WinWedge", "COM2") ' open a link to the Wedge
MyData$ = DDERequest(chan, "Field(1)") ' get the data from the Wedge
DDETerminate chan ' close the link
Selection.TypeText MyData$ & vbcr ' enter the data into the open document followed by a carriage return
' add your own code here

End Sub

After you are done entering the code select "Close" from the FILE menu to save the macro.

This example sets up the Wedge to issue a DDE command ([GRABDATA()]) to Word after each data record is received from your serial device. This causes the GRABDATA subroutine to run which then performs a DDERequest back to the Wedge asking for the data in Field(1) in the Wedge Window. This data is returned to Word in the variable: MyData$. Once the data from the Wedge is in a variable, you can do whatever you like with it by including additional code of your own design. This example simply inserts the text in the MyData$ string variable into the open document and then inserts a paragraph marker.

The following code fragment shows how to transmit a string out the serial port from Word:

 

channel = DDEInitiate("WinWedge", "COM2") ' open a link to the Wedge
DDEExecute channel, "[Sendout('Test',13)]" ' transmit "Test" and a carriage return
DDETerminate channel ' close the link

Back to Code Samples