Try OpenEdge Now
skip to main content
Programming Interfaces
External Program Interfaces : Windows Dynamic Data Exchange : DDE example
 

DDE example

The i-ddeex1.p ABL procedure uses the DDE facility to build a Microsoft Excel worksheet of Customer balances from the sports2000 database and display the Customer payables distribution in an Excel pie chart. It incorporates most of the code examples in the previous sections.
The visible DDE frame displays a selection list (DDE History) showing the server, topic, and item name for each exchange as it occurs. (Note the custom selection list delimiter ( | ) used in place of the default comma delimiter that appears in commands.) When you manually change the balance value in cell r4c2 of the worksheet, ABL uses an advise link to retrieve and display the new value in the field labeled Cell R4C2 (Row 4 Col B).
Note: For i-ddeex1.p to work, you must have Excel in your path or specify the full Excel pathname for the prog_name parameter when you run the WinExec procedure to start Excel. This example assumes that you have Excel in the default Microsoft Office directory.
i-ddeex1.p
DEFINE VARIABLE listx   AS CHARACTER NO-UNDO
  VIEW-AS SELECTION-LIST SIZE 36 BY 5.
DEFINE VARIABLE rowi    AS INTEGER   NO-UNDO.
DEFINE VARIABLE sys   AS INTEGER   NO-UNDO.
DEFINE VARIABLE sheet   AS INTEGER   NO-UNDO.
DEFINE VARIABLE itemn   AS CHARACTER NO-UNDO.
DEFINE VARIABLE sty   AS CHARACTER NO-UNDO.
DEFINE VARIABLE ed    AS CHARACTER NO-UNDO VIEW-AS EDITOR SIZE 20 by 2.
DEFINE VARIABLE log_i   AS LOGICAL   NO-UNDO.
DEFINE VARIABLE excelon AS LOGICAL   NO-UNDO.

DEFINE BUTTON bq LABEL "Quit".
DEFINE BUTTON bg LABEL "Start Excel".

DEFINE FRAME MainFrame
  SKIP(1) SPACE(1) bq SPACE(1) bg SPACE(1) SKIP(1)
  SPACE(1) listx LABEL "DDE History" SPACE(1) SKIP(1)
  SPACE(1) ed LABEL "Cell R4C2 (Row 4 Col B)" SKIP(1)
  WITH SIDE-LABELS.

ENABLE ALL WITH FRAME MainFrame TITLE "Worksheet Monitor".
listx:DELIMITER = "|". /* No server commands use "|" */

PROCEDURE WinExec EXTERNAL "kernel32.dll": /* Run Windows application */
  DEFINE INPUT PARAMETER prog_name  AS CHARACTER.
  DEFINE INPUT PARAMETER prog_style AS LONG.
END PROCEDURE.

PROCEDURE LogExchange: /* Log latest DDE in selection list */
  log_i = listx:ADD-LAST(FRAME MainFrame:DDE-NAME + " " +
    FRAME MainFrame:DDE-TOPIC + " " +
    FRAME MainFrame:DDE-ITEM) IN FRAME MainFrame.
END PROCEDURE.

ON CHOOSE OF bq IN FRAME MainFrame DO:
  IF (excelon = FALSE) THEN RETURN.
  DDE ADVISE sheet STOP ITEM "r4c2". RUN LogExchange.
  DDE TERMINATE sheet. RUN LogExchange.
  DDE EXECUTE sys COMMAND "[activate(~"sheet1~")]". RUN LogExchange.
  DDE EXECUTE sys COMMAND "[close(0)]". RUN LogExchange.
  DDE EXECUTE sys COMMAND "[activate(~"chart1~")]". RUN LogExchange.
  DDE EXECUTE sys COMMAND "[close(0)]". RUN LogExchange.
  DDE EXECUTE sys COMMAND "[error(0)]". RUN LogExchange.
  DDE EXECUTE sys COMMAND "[quit()]". RUN LogExchange.
END.
ON CHOOSE OF bg IN FRAME MainFrame DO:
  /* INPUT: 1=normal 2=minimized */
  RUN WinExec ("C:\Program Files\Microsoft Office\Office\Excel /e", INPUT 2).
  excelon = TRUE.

  DDE INITIATE sys FRAME FRAME MainFrame:HANDLE
    APPLICATION "Excel" TOPIC "System". RUN LogExchange.
  IF sys = 0 THEN DO:
    MESSAGE "Excel not available".
    RETURN.
  END.
  DDE EXECUTE sys COMMAND "[new(1)]". RUN LogExchange.
  DDE INITIATE sheet FRAME FRAME MainFrame:HANDLE
    APPLICATION "Excel" TOPIC "Sheet1". RUN LogExchange.
  DDE SEND sheet SOURCE "Name" ITEM "r1c1". RUN LogExchange.
  DDE SEND sheet SOURCE "Balance" ITEM "r1c2". RUN LogExchange.
  DDE SEND sheet SOURCE "State" ITEM "r1c3". RUN LogExchange.
  rowi = 2.

  FOR EACH Customer NO-UNDO
    WHERE Customer.Balance < 5000 BY Customer.Balance:
    itemn = "R" + STRING(rowi) + "C1".
    DDE SEND sheet SOURCE Customer.Name ITEM itemn. RUN LogExchange.
    itemn = "R" + STRING(rowi) + "C2".
    DDE SEND sheet SOURCE STRING(Customer.Balance) ITEM itemn.
      RUN LogExchange.
    itemn = "R" + STRING(rowi) + "C3".
    DDE SEND sheet SOURCE STRING(Customer.State) ITEM itemn. RUN LogExchange.
    rowi = rowi + 1.
  END.

  DDE EXECUTE sheet COMMAND "[select(~"C1:C2~")]". RUN LogExchange.
  DDE EXECUTE sys COMMAND "[column.width(,,,3)]". RUN LogExchange.
  DDE EXECUTE sys COMMAND "[format.number(~"$#,##0~")]". RUN LogExchange.
  DDE EXECUTE sheet COMMAND "[select(~"C1:C2~")]". RUN LogExchange.
  DDE EXECUTE sys COMMAND "[new(2,1,1)]". RUN LogExchange.
  DDE EXECUTE sys COMMAND "[activate(~"chart1~")]". RUN LogExchange.
  DDE EXECUTE sys COMMAND "[gallery.3d.pie(3)]". RUN LogExchange.
  DDE EXECUTE sys COMMAND "[app.restore()]". RUN LogExchange.
  DDE EXECUTE sys COMMAND "[arrange.all()]". RUN LogExchange.
  
  DDE ADVISE sheet START ITEM "r4c2". RUN LogExchange.
END. /* ON CHOOSE of bg IN FRAME MainFrame */

ON DDE-NOTIFY OF FRAME MainFrame DO:
  DDE GET sheet TARGET sty ITEM "r4c2". RUN LogExchange.
  sty = SUBSTR(sty, 1, 20). /* Drop the CR/LF */
  ed:SCREEN-VALUE IN FRAME MainFrame = sty.
END.

WAIT-FOR CHOOSE OF bq IN FRAME MainFrame.