Dynamic Data Exchange (DDE) Excel

Project: A DDE-enabled PC application, such as Excel, Access or Visual Basic, has a need to interact with a host session to communicate with a host application. A minimal implementation would require the ability to get a screen capture of what is currently displayed in the session, and to send characters or keystrokes to the session.

Algorithm: DDE support in DynaComm is implemented via script, whether as a DDE Client or DDE Server. In this example, DynaComm creates documents in Excel.

Product used in this example: DynaComm Connectivity Series®

Example Script:

show
linenumbers

title "DDE 2 Excel Demo"
$title = "DDE 2 Excel Demo"

; ============================================================
; Try to access Excel and if can't, launch
; ============================================================
%channel = 0
%channels = 0

access "Excel" "system" %channel
if error()
begin
perform findAndLaunchExcel()
access "Excel" "system" %channel
if error()
begin
$title = "DDE Demo Error!!"
$m1 = "Excel could not be found."
$m2 = ""
$m3 = ""
$m4 = "Please start Excel and run this script again."
$m5 = ""
$m6 = ""
$m7 = "Pressing OK at this time will stop this script."
dialog $title
message $m1
message $m2
message $m3
message $m4
message $m5
message $m6
message $m7
button "OK" resume
dialog end
wait resume
cancel
end
end

access cancel %channel

; ============================================================
; Try to access sheet DDEDEMO.XLS and if cannot create sheet
; ============================================================

$todayFN = "DDEDEMO"
perform creatFileDtTm($todayFN)

access "Excel" $todayFN %channel
if error
begin
access "Excel" "system" %channel
instruct %channel '[NEW(1)]'
instruct %channel '[SAVE.AS("' | $todayFN | '",1,"",FALSE)]'
access cancel %channel
access "Excel" $todayFN %channel
end

; ============================================================
; Send Excel tables of info
; ============================================================

table define 9 fields char 25 int 5 char 1
%tabCols = 3

; check for data file...
table load 9 from directory(memo) | "table9.txt" AS text
$recCount = RESULT()
if(length($recCount) > 9)
begin
perform fill_table() ; create tables with data
table define 9 fields char 25 int 5 char 1 ; reset
end

table load 9 from directory(memo) | "table9.txt" AS text
$recCount = RESULT()

$rowsAndColumns = "R1C1:" | "R" | $recCount | "C" | str(%tabCols)

dialog (,,400) "Data to send to Excel..."
message RESULT() | " records"
message $rowsAndColumns
message $todayFN
dialog end
wait delay "5"

table send 9 to %channel $rowsAndColumns sylk
table close 9

wait delay "7"

instruct %channel '[SAVE]'
instruct %channel '[QUIT]'
return

; ============================================================
; Create the tables which will transport the data to Excel
; ============================================================
*fill_table()
@R9.1 = "mike"
@R9.2 = "42"
@R9.3 = "M"
record write 9
@R9.1 = "jane"
@R9.2 = "39"
@R9.3 = "F"
record write 9
@R9.1 = "john"
@R9.2 = "36"
@R9.3 = "M"
record write 9
@R9.1 = "sue"
@R9.2 = "32"
@R9.3 = "F"
record write 9
@R9.1 = "bob"
@R9.2 = "55"
@R9.3 = "M"
record write 9
@R9.1 = "betty"
@R9.2 = "31"
@R9.3 = "F"
record write 9
@R9.1 = "alan"
@R9.2 = "50"
@R9.3 = "M"
record write 9

table save 9 to directory(memo) | "table9.txt" AS text
table close 9
return

*findAndLaunchExcel()
$EXCEL_PATH = "C:\Program Files\Microsoft Office\Office12\"
$EXCEL_PATH = $EXCEL_PATH | "excel.exe"

Launch $EXCEL_PATH
wait delay "4"
return

;;;;;;;;;;;;;;;;;;;;
;DLL util version...
$DLLPATHLOC = getappconfig("USERTASKDIR")
$DLLRUNPATH = $DLLPATHLOC | "\xxxUtil.dll"
library load $DLLRUNPATH %libID
if(%libID < 32)
begin
dialog (,,300) "Fatal Error: " | $DLLRUNPATHwww
message "Unable to load!!!"
button "OK" resume
dialog end
wait resume
return
end

$winwordpath = "whereisword"
library call %libID "getWinWordPath" ($winwordpath)
$WINWORD_EXE_LOC = $winwordpath

%x = 1
while(%x > 0)
begin
%e = %x ; remember last backslash
%x = pos($WINWORD_EXE_LOC, "\", %x + 1)
end

$EXCEL_PATH = substr($WINWORD_EXE_LOC, 1, %e)
$EXCEL_PATH = $EXCEL_PATH | "excel.exe"

Launch $EXCEL_PATH
wait delay "4"
return

*creatFileDtTm($newFN)
$newFN = $newFN | date() | time()
$tempFN = ""
%x = 1
while(%x <= length($newFN))
begin
$c = substr($newFN,%x,1)
if $c <> "." AND $c <> ":" AND
$c <> " " AND $c <> "/" $tempFN = $tempFN | $c
increment %x
end
$newFN = $tempFN | ".XLS"
return

In this example, DynaComm creates an Excel file and loads data into it. It then saves the XLS file with a filename based on the current date and time.

For example the file DDEDEMO0719201230014PM.XLS would contain this data:
mike 42 M
jane 39 F
john 36 M
sue 32 F
bob 55 M
betty 31 F
alan 50 M