Loading the VBScript Program

Here’s a quick hack job that lets me load the calculator’s VBScript code from a disk file. As you can see in Listing 13.10, it isn’t very pretty, but it works.

Listing 13.10: LoadCode Script Routine in Programmable Calculator

Private Sub LoadCode()
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim s As String
Dim t As TextStream
Dim x() As String
On Error Resume Next
Set t = fso.OpenTextFile(App.Path & “\calc.mod”)
s = t.ReadAll
t.Close
x = Split(s, “‘~”)
For i = 0 To MaxButtons - 1
   KeyButton(i).Caption = Trim(Mid(x(i + 1), 9, 10))
   j = InStr(1, x(i + 1), “Sub “, vbTextCompare)
   Code(i) = Mid(x(i + 1), j, Len(x(i + 1)) - j + 1)
   k = InStr(j + 4, x(i + 1), “()”, vbTextCompare)
   CodeName(i) = Trim(Mid(x(i + 1), j + 4, k - j - 4))
   ScriptControl1.AddCode Code(i)
Next i
End Sub

I start this routine by declaring a bunch of local variables and inserting my usual On Error Resume Next statement. As I did with the VBScript updating routine, I’m going to let the Error event perform the work if an error arises.

I open the “calc.mod” file (the beginning of this file is shown in Listing 13.11) using the OpenTextFile method of the FileSystemObject and load the file into a memory with a ReadAll method. This is followed by a call to the Split function to split the contents of the file into individual subroutines using the array x. Each subroutine is separated by a marker consisting of an apostrophe followed by a tilde (‘~). This is a valid comment in Visual Basic, so it doesn’t hurt if it is included as part of the program.

Listing 13.11: Start of the Calc.mod File

‘ Revised: 6/14/98 3:01:11 PM
‘~
‘Key:          7
Sub Press7()
Display.Text = Display.Text & “7”
End Sub
‘~
‘Key:          4
Sub Press4()
Display.Text = Display.Text & “4”
End Sub
‘~

The section before the first marker I call the file’s header. Basically, I throw this information away when I load the file; however, it is useful to have when looking at the raw file. This information will be loaded into x(0). Immediately following the marker is the information for the first button, KeyButton(0). Then the next marker will appear, followed by the next block of code. This will continue in the same order as the buttons until all 20 buttons have been defined.

The information associated with a single button has some strict formatting requirements. The marker is on a line by itself. A line containing the text to be displayed as button’s caption immediately follows the marker. The characters in positions nine through eighteen will be used at the key’s caption.

The next line of the file contains the subroutine definition. The format is the typical definition Visual Basic subroutine definition like sub xxx(). To parse this line, I look for the keyword sub then I look for the empty parentheses (). The characters in between (xxx) are the name of the subroutine.

Once I have collected all of this information, I simply use the AddCode method to install the entire block of code into the scripting engine. The scripting engine ignores the lines of comments, which is why I formatted the additional information as comments.

NOTE: One little, two little, three little subroutines: After a little experimenting, I found that the AddCode and the ExecuteStatement methods are relatively insensitive to the amount of code processed. Each line of code must end in a carriage return/line feed pair, including the last. Beyond that, the number of subroutines or statements doesn’t appear to matter when using the ScriptControl.

© 1998 SYBEX Inc. All rights reserved.