Sheerpower®
A Guide to the Sheerpower Language


Previous Contents Index


Chapter 12
Sheerpower and DDE

FORMAT:


  SET SYSTEM, SYMBOL '@' + str_expr1: VALUE str_expr2 
  ASK SYSTEM, SYMBOL '@' + str_expr1: VALUE str_expr2 
  
  where str_expr1 is: @program_name, data_for_program 

EXAMPLE:

Example 12-1 Accessing Microsoft Excel Via DDE

// Note:  Excel's DDE interface is a bit odd in that 
//        to reference cell B1, we use "R1C2"  (row 1, column 2) 
// This example is also found in \sheerpower\samples folder 
 
dde$ = '@excel,ddetest.xls,' 
 
xlsfile$ = filespec$("sheerpower:samples\ddetest.xls") 
pass url: xlsfile$  // open our spreadsheet 
 
cell$ = 'r1c1' 
data$ = 'Nifty' 
set system, symbol dde$+cell$: value data$ 
 
cell$ = 'r2c1' 
data$ = day$ 
set system, symbol dde$+cell$: value data$ 
 
for x = 1 to 50 
  set system, symbol dde$ + 'r'+str$(x)+'c2': value str$(x) 
next x 
 
ask system, symbol '@': value st$ 
z0 = pos (st$, '0') 
if z0 > 0 then 
  message error: 'Got an error on command# '; z0 
end if 
 
end 

Note

Accessing Excel through Sheerpower's DDE interface has only been tested on English versions of Excel.

PURPOSE:

2 or more programs running simultaneously that support DDE can exchange information and commands. For example, Sheerpower can exchange information and commands with Microsoft Word or Excel.

Each application controlled by Sheerpower's DDE interface has its own unique way of being controlled. You must be familiar with the application's DDE commands that you want to control with Sheerpower. These commands are usually outlined in the application's documentation on DDE.

DESCRIPTION:

The set system, symbol statement sends a command or data to the receiving application.

The ask system, symbol statement asks the application for data to be returned.

The use of the PASS NOWAIT statement is the best way to start an application to control.

The PASS URL statement opens the application if it is not already open. Use the PASS NOWAIT statement to start or open an application that doesn't have a document associated with it.

12.0.1 Checking DDE Syntax

If you receive an error message that reads:


  Bad syntax for execute command 

check to make sure the syntax follows these rules:

For example, the following syntax to make a DDE connection to Quattro spreadsheets is incorrect:


  a$ = "{FileNew}" 
  set system, symbol "@QPW,System": value a$ 
The correct syntax would be:


  a$ = "[FileNew]"  
  set system, symbol "@QPW,System": value a$ 

12.0.2 Accessing Microsoft Word Via DDE

Below are some examples of accessing Microsoft Word via DDE using Sheerpower:

Example 12-2 Accessing Microsoft Word Via DDE

// bookmarks.spsrc 
// written by Mark S. Johnson, CMH 
 
ask system: user user$ 
pass url: "c:\sheerpower\myname.doc" 
delay 1 
// PASS URL statement opens up the Word document 
 
ask system, symbol "@WINWORD,System,Topics": value topics$ 
//Get the available Word topics for the current document 
 
selection$ = piece$(topics$,2,chr$(9)) 
//The name of the current document is the second topic 
 
print "The current Word document is ";selection$ 
 
set system, symbol "@WINWORD," & selection$ & ",user": value user$ 
//Store the user name into the bookmark named "user" 
 
stop 

Example 12-3 More Microsoft Word DDE Commands

//Open a word file whose name in stored in new_word_file$ 
word_command$ = '[FileOpen("' & new_word_file$ & '")]' 
set system, symbol "@WINWORD,System": value word_command$ 
 
//Commands enclosed in brackets [] are Word Basic commands 
 
//Close a Word document whose name is in selection$ 
set system, symbol "@WINWORD," & selection$: value "[FileClose 1]" 
//Close and save the file is set by parameter "1". Using "2" as the 
//parameter closes, but does not save the file. A parameter of "0" prompts 
//the user. 
 
//Exit Word 
set system, symbol "@WINWORD," & selection$: value "[FileExit 2]" 
//Here the parameter "2" says quit and don't save. A parameter of "1" 
//says quit and save. If the parameter is left out or is "0", the user is 
//prompted. 

12.1 Read/Write to the Windows Registry

FORMAT:


  ASK SYSTEM, SYMBOL "\" + REGISTRY_KEY 
  SET SYSTEM, SYMBOL "\" + REGISTRY_KEY 

EXAMPLE:

Example 12-4 Read/Write the Windows Registry

// Fetch the Product ID from the registry 
  rkey$ = '\hkey_local_machine\Software\Microsoft\Windows' + 
                  '\CurrentVersion\ProductId' 
  ask system, symbol rkey$: value kvalue$ 
  print 'Product ID: '; kvalue$ 
  end 
 
 
Product ID: 55555-OEM-1113333-44444 


Chapter 13
Exception Handling

Exception handling routines intercept runtime exceptions and execute a block of code which handles them. If there is no exception handler, SheerPower returns an exception message specifying what the exception was and where it occurred. SheerPower stops program execution or tries the offending statement again.

There are two types of exception handlers: attached handlers and detached handlers.

For an attached handler, a WHEN EXCEPTION IN statement is used. WHEN EXCEPTION IN puts the handler (the block of code to execute) right after the block of code it protects.

For a detached handler, the statement WHEN EXCEPTION USE is used. When an exception occurs in the protected block, WHEN EXCEPTION USE calls a handler routine located in some other part of the program. The same handler routine can be used by any number of WHEN EXCEPTION USE statements and can be placed anywhere in a program.

This section explains exception handlers. It also describes the handling statements--RETRY, CONTINUE and EXIT HANDLER.

The following functions are also related to exception handling:

For a full description of these functions, see Section 6.8, Debugging and Exception Handling Functions.

13.1 CAUSE EXCEPTION

FORMAT:


        CAUSE EXCEPTION exception_number 

EXAMPLE:

Example 13-1 CAUSE EXCEPTION

  do
    input 'Select a number between 1 and 10': no 
    if  no < 1 or no > 10  then cause exception 1001
    repeat do
  end do
  end
 
        
Select a number between 1 and 10? 8 
Select a number between 1 and 10? 99 
Illegal number at 10.2 

PURPOSE:

CAUSE EXCEPTION is used to generate an exception under specific conditions.

DESCRIPTION:

CAUSE EXCEPTION causes the specified exception to occur. exception_number can be any integer expression. When SheerPower executes the CAUSE EXCEPTION statement, it generates the exception specified. See Section C.2, Exceptions for a list of exception messages.

13.2 WHEN EXCEPTION IN

FORMAT:


        WHEN EXCEPTION IN 
          --- 
          ---  protected block 
          --- 
        USE 
          --- 
          ---  handler 
          --- 
        END WHEN 

EXAMPLE:

Example 13-2 WHEN EXCEPTION IN/USE/END WHEN

  input 'Your name, please': name$ 
  when exception in
    input 'How old are you': age 
  use
    print 'Not a valid age' 
    retry
  end when
  print
  print name$; ' is'; age 
  end
 
        
Your name, please? Tester 
How old are you? 3x 
Not a valid age 
How old are you? 35 
 
Tester is 35 

PURPOSE:

WHEN EXCEPTION IN is used to catch runtime exceptions and resolve them within a program when the code handling the exception is to be next to the code being protected.

DESCRIPTION:

WHEN EXCEPTION IN begins the protected block of code. Everything between the WHEN EXCEPTION IN and USE statements constitute the section of code protected by the handler---the protected block.

USE begins the handler routine. Everything between the USE and the END WHEN statements constitutes the handler. If an exception occurs in the protected block, the handler code is executed. END WHEN ends the routine.

13.3 WHEN EXCEPTION USE

FORMAT:


        WHEN EXCEPTION USE handl_name 
          ---               
          ---  protected block 
          --- 
        END WHEN 
                 . 
                 . 
                 . 
        HANDLER handl_name 
          --- 
          ---  handler 
          --- 
        END HANDLER 


Previous Next Contents Index