;DDEEXAMP.WAS v1.0 - Example DDE Client script

;*****************************************************************************
;*                                                                           *
;* DDEEXAMP.WAS                                                              *
;* Copyright (C) 1999 Symantec Corp.			  			                           *
;* All rights reserved.                                                      *
;*                                                                           *
;* DDEEXAMP.WAS provides a working example of using PROCOMM PLUS 3.0 for     *
;* Windows ASPECT to establish and control a DDE link with Microsoft Excel   *
;* 5.0.  The script manipulates an example spreadsheet called SODA.XLS.      *
;*                                                                           *
;* This ASPECT SCRIPT is intended only as a sample of ASPECT programming.    *
;* Symantec makes no warranty of any kind, express or implied, including     *
;* without limitation, any warranties of merchantability and/or fitness      *
;* for a particular purpose.  Use of this program is at your own risk.       *
;*****************************************************************************


;*****************************************************************************
;*   MACRO DEFINITIONS                                                       *
;*****************************************************************************
#define FULLEXCELNAME "X:\EXCEL5\EXCEL.EXE"     ;full name and path of Excel
#define FULLSPREADSHEETNAME "C:\EXCEL\SODA.XLS" ;full name path of Spreadsheet


;*****************************************************************************
;*   GLOBAL VARIABLES                                                        *
;*****************************************************************************
string OunceStr="---"          ;String equivalent of Ounces in Dialog Box
string CalorieStr="---"        ;String equivalent of Calories
string CanStr="---"            ;String equivalent of TotalCans
string ExcerciseStr="---"      ;String equivalent of TotalExcercise
integer GoodLink=0             ;boolean to show success or failure of Link
integer ExcelHandle            ;Handle to EXCEL.EXE
long SysChan = 0               ;Identifier to Excel system.
long DDEChan = 0               ;Identifier to DDE channel.
string DummyString             ;Dummy variable name for $DDEADVISE statement
                               ; (we're not using the feature that tells which
                               ;  variable has been changed).


;*****************************************************************************
;*                                                                           *
;* MAIN                                                                      *
;* The Main procedure calls ShowMain to display the main dialog box and then *
;* loops indefinitely, waiting for user interaction.  When user interaction  *
;* occurs, Main calls ProcessMain to handle the user interaction.            *
;*                                                                           *
;* Calls: ShowMain, ProcessMain                                              *
;* Modifies globals: None                                                    *
;*                                                                           *
;*****************************************************************************

proc main

   when dialog 0 call ProcessMain     ;process user input
   when $DDEADVISE call UpdateValues  ;take action if Excel value R3C1 changes
   ShowMain()                         ;display the main dialog box
   while 1                            ;loop indefinitely
   endwhile
endproc



;*****************************************************************************
;*                                                                           *
;* CleanUp()                                                                 *
;* The procedure CleanUp destroys the main dialog box, kills Excel if it is  *
;* still running, and exits the script.                                      *
;*                                                                           *
;* Calls: None                                                               *
;* Called by: ProcessMain                                                    *
;* Modifies globals: None                                                    *
;*                                                                           *
;*****************************************************************************

proc CleanUp

   dlgdestroy 0 OK             ;kill the main dialog box
   if taskexists ExcelHandle   ;if Excel is still running...
      taskexit ExcelHandle     ;exit from Excel
      taskactivate $PWTASK     ;bring PROCOMM PLUS to the front
   endif
   exit                        ;exit the script
endproc



;*****************************************************************************
;*                                                                           *
;* ReportExcercise()                                                         *
;* The procedure ReportExcercise prompts the user for an amount of excercise *
;* completed (minutes).  The procedure then grabs the total ounces of Soda   *
;* from the spreadsheet, subtracts the appropriate amount (.8 ounces per     *
;* minute of excercise, and pokes the adjusted value of ounces back into the *
;* spreadsheet.  It then saves the spreadsheet.                              *
;*                                                                           *
;* Calls: None                                                               *
;* Called by: ProcessMain                                                    *
;* Modifies globals: ExcerciseStr                                            *
;*                                                                           *
;*****************************************************************************

proc ReportExcercise
float TotalOunces                                 ;Value of Ounces from Excel
float Excercise                                   ;Amount of Excercise input 
   
   if GoodLink                                    ;if a Link was established
      if sdlginput "No pain, no gain..." "How many minutes did you just \
excercise" ExcerciseStr
                                                  ;get user input (minutes) 
         atof ExcerciseStr Excercise              ;change string to a float
         taskactivate ExcelHandle                 ;bring Excel to the front
         dderequest DDEChan "R3C1" TotalOunces    ;get TotalOunces from Excel
         TotalOunces=TotalOunces-(Excercise * .8) ;calculate New TotalOunces
         ddepoke DDEChan "R3C1" TotalOunces       ;poke new value into Excel
         pause 1                                  ;wait a second
         if ddeinit SysChan "excel" "system"      ;init link to Excel System
            ddeexecute SysChan "[SAVE()]"         ;save the spreadsheet.
         endif
      endif
      taskactivate $PWTASK                        ;bring PROCOMM PLUS to front
   else                                           ;otherwise (No link exists)
      usermsg "No DDE link exists..."             ;inform that no link exists
   endif
endproc



;*****************************************************************************
;*                                                                           *
;* AddOunces()                                                               *
;* The procedure AddOunces prompts the user for an amount of ounces of soda  *
;* consumed.  The procedure then grabs the total ounces of Soda from the     *
;* spreadsheet, adds the new amount, and pokes the adjusted value of ounces  *
;* back into the spreadsheet.  It then saves the spreadsheet.                *
;*                                                                           *
;* Calls: None                                                               *
;* Called by: ProcessMain                                                    *
;* Modifies globals: OunceStr                                                *
;*                                                                           *
;*****************************************************************************

proc AddOunces
float TotalOunces                              ;Value of Ounces from Excel
float Ounces                                   ;Value of Ounces input by user
   
   if GoodLink                                 ;if a DDE Link was established
      if sdlginput "Add some calories, bud..." "How many ounces of SODA did\
you just drink" OunceStr
                                               ;get user input (oz. consumed)
         atof OunceStr Ounces                  ;change string to a float value
         taskactivate ExcelHandle              ;bring Excel to the front
         dderequest DDEChan "R3C1" TotalOunces ;get TotalOunces from Excel
         TotalOunces=TotalOunces+Ounces        ;calculate New TotalOunces
         ddepoke DDEChan "R3C1" TotalOunces    ;poke new value into Excel
         pause 1
         if ddeinit SysChan "excel" "system"   ;init link to Excel System
            ddeexecute SysChan "[SAVE()]"      ;save the spreadsheet.
         endif
      endif
      taskactivate $PWTASK                     ;bring PROCOMM PLUS to front
   else                                        ;otherwise...  (No link exists)
      usermsg "No DDE link exists..."          ;inform that no link exists
   endif
endproc



;*****************************************************************************
;*                                                                           *
;* AddCans()                                                                 * 
;* The procedure AddCans prompts the user for a number of cans of soda       *
;* consumed.  The procedure then grabs the total ounces of Soda from the     *
;* spreadsheet, adds the appropriate amount of ounces to the total (12       *
;* ounces per can), and pokes the adjusted value of ounces back into the     *
;* spreadsheet.  It then saves the spreadsheet.                              *
;*                                                                           *
;* Calls: None                                                               *
;* Called by: ProcessMain                                                    *
;* Modifies globals: CanStr                                                  *
;*                                                                           *
;*****************************************************************************

proc AddCans
float TotalOunces                              ;Value of Ounces from Excel
float Cans                                     ;Value of Cans input by user

   if GoodLink                                 ;if a DDE Link was established
      if sdlginput "Another Can???" "How many cans of SODA did you just \
drink" CanStr
                                               ;get user input (cans consumed)
         atof CanStr Cans                      ;change string to a float value
         taskactivate ExcelHandle              ;bring Excel to the front
         dderequest DDEChan "R3C1" TotalOunces ;get TotalOunces from Excel
         TotalOunces=TotalOunces+(Cans*12)     ;calculate New TotalOunces
         ddepoke DDEChan "R3C1" TotalOunces    ;poke new value into Excel
         pause 1
         if ddeinit SysChan "excel" "system"   ;init link to Excel System
            ddeexecute SysChan "[SAVE()]"      ;save the spreadsheet.
         endif
      endif
      taskactivate $PWTASK                     ;bring PROCOMM PLUS to front
   else                                        ;otherwise (No DDE link exists)
      usermsg "No DDE link exists..."          ;inform user no link exists
   endif
endproc



;*****************************************************************************
;*                                                                           *
;* InitDDELink()                                                             *
;* The procedure InitDDELink runs MS Excel and attempts to initiate a DDE    *
;* link.  The procedure will try to initiate the link three times, with a 5  *
;* second pause between attempts.  This way, if Excel isn't completely up    *
;* when the script first attempts to make the connection, the script won't   *
;* immediately fail, but instead try a couple more times.  Once the link is  *
;* established, the script brings PROCOMM PLUS back to the front.            *
;*                                                                           *
;* Calls: None                                                               *
;* Called by: ProcessMain                                                    *
;* Modifies globals: ExcelHandle, DDEChan                                    *
;*                                                                           *
;*****************************************************************************

proc InitDDELink
string CommandString                           ;holds the full command line
integer Counter=0                              ;counts 3 tries to init link
   
   strfmt CommandString "%s %s" FULLEXCELNAME FULLSPREADSHEETNAME
                                               ;format the run command
   run CommandString MAXIMIZED ExcelHandle     ;run Excel and SODA.XLS
   while Counter<3                             ;while we haven't tried 3 times
      ddeinit DDEChan "EXCEL" "SODA.xls"       ;init the DDE Link to SODA.XLS      
      if FAILURE                               ;if we couldn't get a link...
         pause 5                               ;give Excel a bit of time
                                               ;before trying again
         counter++                             ;increment # of tries
      else                                     ;otherwise (we got a good link)
         GoodLink=1                            ;set GoodLink (boolean) to 1
         taskactivate $PWTASK                  ;bring PROCOMM PLUS to front
         ddeadvise DDEChan "R3C1" DummyString  ;set up DDEADVISE to inform the
                                               ;script if cell R3C1 changes
         UpdateValues()                        ;update the values in the main
                                               ;dialog box
      exitwhile                                ;get out of the while loop
      endif
   endwhile
endproc



;*****************************************************************************
;*                                                                           *
;* UpdateValues()                                                            *
;* The procedure UpdateValues grabs the values of TotalOunces, TotalCans,    *
;* TotalCalories, and TotalExcercise from the Spreadsheet.  It then converts *
;* those numeric values to their string equivalents and updates the main     *
;* dialog box with the new values.                                           *
;*                                                                           *
;* Calls:                                                                    *
;* Called by: Main, InitDDELink                                              *
;* Modifies globals: OunceStr, CanStr, CalorieStr, ExcerciseStr              *
;*                                                                           *
;*****************************************************************************

proc UpdateValues
float TotalOunces                          ;Value of Ounces from Spreadsheet
float Calories                             ;Value of Calories from Spreadsheet
float TotalCans                            ;Value of Cans from Spreadsheet
float TotalExcercise                       ;Excercise needed to burn all 
                                           ; (from Spreadsheet)

   dderequest DDEChan "R3C1" TotalOunces   ;get TotalOunces from spreadsheet
   ftoa TotalOunces OunceStr               ;change it from float to string
   dderequest DDEChan "R3C2" TotalCans     ;get TotalCans from spreadsheet
   ftoa TotalCans CanStr                   ;change it from float to string
   dderequest DDEChan "R3C3" Calories      ;get Calories from spreadsheet
   ftoa Calories CalorieStr                ;change it from float to string
   dderequest DDEChan "R3C4" TotalExcercise;get TotalExcercise from spreadsheet
   ftoa TotalExcercise ExcerciseStr        ;change it from float to string
   dlgupdate 0 12 15                       ;update the main dialog box to 
                                           ; reflect the new values
endproc



;*****************************************************************************
;*                                                                           *
;* ProcessMain()                                                             *
;* The procedure ProcessMain processes user input to the main dialog box and *
;* takes appropriate action according to that input.                         *
;*                                                                           *
;* Calls: AddOunces, AddCans, ReportExcercise, InitDDELink, CleanUp          *
;* Called by: Main                                                           *
;* Modifies globals: None                                                    *
;*                                                                           *
;*****************************************************************************

proc ProcessMain
integer Status                       ;holds value of user input

   dlgevent 0 Status                 ;get value of user input
   switch Status                     ;depending on value of Status
      case 16                        ;if they clicked "Add" ounces,
         AddOunces()                 ;run the AddOunces procedure
      endcase
      case 17                        ;if they clicked "Add" cans,
         AddCans()                   ;run the AddCans procedure
      endcase
      case 18                        ;if they clicked on "Report Excercise"
         ReportExcercise()           ;run the ReportExcercise procedure
      endcase
      case 48                        ;if they clicked on "Kill Excel"
         taskexit ExcelHandle        ;exit MS Excel
         disable dlgctrl 0 16 18     ;disable the "Add" and "Report" buttons
         disable dlgctrl 0 48        ;disable the "Kill Excel" button
         enable dlgctrl 0 49         ;enable the "Init DDE" button
      endcase
      case 49                        ;if they clicked on "Init DDE"    
         disable dlgctrl 0 49        ;disable the "Init DDE" button
         enable dlgctrl 0 16 18      ;enable the "Add" and "Report" buttons
         enable dlgctrl 0 47 48      ;enable the "Kill Excel" button
         InitDDELink()               ;call the procedure to init the DDE Link
      endcase
      case 50                        ;if they clicked on the "Exit" button
         CleanUp()                   ;run the CleanUp procedure (to exit)
      endcase
   endswitch
endproc



;*****************************************************************************
;*                                                                           *
;* ShowMain()                                                                *
;* The procedure ShowMain displays the main dialog box and disables the      *
;* the following dialog controls:  16, 17, 18, 47, 48 (the controls are      *
;* enabled when a successful DDE Link is made).                              *
;*                                                                           *
;* Calls: None                                                               *
;* Called by: Main                                                           *
;* Modifies globals: None                                                    *
;*                                                                           *
;*****************************************************************************

proc ShowMain
   
   dialogbox 0 62 41 311 224 27 "DDE Example Script"
      groupbox 3 4 85 304 100 "Spreadsheet Data"
      text 4 89 100 58 8 "Total Cans of" center
      text 5 12 100 58 8 "Total Ounces of" center
      text 6 165 100 58 8 "Soda Calories" center
      text 7 241 100 58 8 "Excercise Minutes" center
      groupbox 8 8 94 65 83
      groupbox 9 85 94 65 83
      groupbox 10 160 94 66 83
      groupbox 11 239 94 65 83
      text 12 12 135 58 8 OunceStr center
      text 13 242 135 58 8 ExcerciseStr center
      text 14 89 135 58 8 CanStr center
      text 15 180 135 29 8 CalorieStr center
      pushbutton 16 21 159 40 11 "Add"
      pushbutton 17 98 159 40 11 "Add"
      pushbutton 18 243 159 58 11 "Report Excercise"
      pushbutton 50 160 193 40 27 "Exit Script" ok
      groupbox 19 178 129 33 18
      pushbutton 49 109 193 40 12 "Init DDE"
      pushbutton 48 109 208 40 12 "Kill Excel"
      text 20 10 108 58 8 "Soda in Body ¹" center
      text 21 89 108 58 8 "Soda in Body ¹" center
      text 22 242 108 58 8 "To Burn Calories ³" center
      text 23 165 108 58 8 "In Body ²" center
      ftext 1 16 8 279 68 "DDEEXAMP.DAT"
   enddialog
   disable dlgctrl 0 16 18                    ;disable the "add" buttons
   disable dlgctrl 0 48                       ;disable the "Kill Excel" button
endproc
