Converting PerfectScript Product Commands to VBA Format
VBA uses the same product commands as a PerfectScript macro uses. But while the actual commands are basically the same in VBA as they are for PerfectScript, there are several differences that you'll need to get used to when learning VBA.
For example, in PerfectScript, we surround parameters with parentheses.
- The parentheses are not needed in VBA, although I recommend using them anyway.
- A space should separate the command name and the first parameter.
- In PerfectScript, the semicolon separates parameters in the same command. In VBA, we use a comma.
- In VBA, each PerfectScript command must include the PerfectScript identifier before the command name, and separated from it with a period (.). For example, PerfectScript.Keytype ("This is the text to type.")
Enumerations
VBA does not recognize the standard enumerations of WordPerfect product commands. For example, Display(on!) uses the enumeration On!. VBA has no idea what On! means. You'll need to use the VBA enumerator, or the numeric value instead of the enumeration. You can get the numeric value of enumerations in the Command Inserter. Make sure you turn the Show enumeration values option on in the Browser tab in PerfectScript Preferences so that the enumeration value will be displayed in the Command Inserter.
You can get the VBA enumerator as you type the command in the VBA code window. A help tip will pop up after you've typed the command name, followed by a space. This tip will show you the syntax, and a list of the possible values will appear for each parameter. Generally, the VBA enumerator will include the name of the command itself, and the parameter that the enumerator goes with. For example, the State enumerator On! of the Display command is On_Display_State. Its value is the same as On! in PerfectScript.
| PerfectScript |
VBA |
| Display(on!) |
PerfectScript.Display (On_Display_State) |
| MacroStatusPrompt(on!; "test") |
PerfectScript.MacroStatusPrompt (On_MacroStatusPrompt_State, "test") |
| FileOpen(vFile; 4) |
PerfectScript.FileOpen (vFile, WordPerfect_6_7_8_FileOpen_Format) |
System Variables
In PerfectScript we are familiar with environment variables that begin with a question mark. In VBA, the question mark is replaced with the suffix "env." For example ?BlockActive has become envBlockActive.
| PerfectScript |
VBA |
| ?BlockActive |
envBlockActive |
| ?LeftCode |
envLeftCode |
| ?RightChar |
envRightChar |
Reserved VBA Words
VBA includes a few commands that are also WordPerfect product command names. You'll need to convert the PerfectScript command to the VBA version in these cases.
| PerfectScript |
VBA |
| Type("Test") |
PerfectScript.KeyType ("Test") |
| Print (FullDocument!) |
PerfectScript.WPPrint (FullDocument_Print_Action) |
Concatenating Variables
In PerfectScript, we used the plus sign (+) to concatenate two or more variables or strings. In VBA, we use an ampersand (&).
| PerfectScript |
VBA |
| vString3:=vString1 + vString2 |
vString3 = vString1 & vString2 |
Wrapping a Long Line of Code
In PerfectScript, a long line of code would automatically wrap to the next line and would compile normally. In VBA, long code lines do not wrap automatically. If you want to move part of the line of code to the next line to make the code more readable, you must use an underscore character at the point where you want to wrap the code.
| PerfectScript |
VBA |
| Type("This is a long line of text that will automatically wrap at the end of the line.") |
PerfectScript.KeyType "This is a long line of text " & _
"that I want to wrap to the next line. I must use " & _
"the ampersand to concatenate, and the underscore " & _
"to continue the line of code." |
Declaring Variables
Unlike PerfectScript, in VBA you must declare variables and their type before assigning a value and using the variable. This is accomplished with the DIM statement.
Dim vText As String
Dim vNumber As Integer
Dim vBool As Boolean
Dim vVar As Variant
Once a variable is declared, it can have a value assigned to it.
Private and Public Subroutines
VBA has private and Public subroutines. A private subroutine can be used only in the current module. Most routines, including the event handlers, will be private. Like PerfectScript's Local variables, variables that are set in a private routine cannot be accessed from other routines.
Private Sub cmdOKBttn_Click()
Dim vAge as Integer
vAge = txtAge.Text
PerfectScript.KeyType vAge
End Sub
Use Public rather than Dim in the General Declarations section to make a variable public (global) to other subroutines.
Public vName As String
---------------------------------------------------------------
Private Sub cmdDisplayNameBttn_Click()
MsgBox vName
End Sub
---------------------------------------------------------------
Private Sub cmdGetNameBttn_Click()
vName = txtName.Text
End Sub
Form Properties
In PerfectScript dialogs, values gathered from the user are assigned to variables that you specify. But the user input for a control is not assigned to the variable until the dialog is dismissed, or a DialogQuery command forces the value to be assigned during a callback.
IN VBA forms, user input is automatically assigned to a property of the control. You can then use that value as a property, or you can assign the value to a variable. For example, a text box on a form has a property called Text. If you named the text box txtUsername, then you could get the information typed into the text box with the property txtUsername.Text.
Dim vUsername as String
vUsername = txtUsername.Text
You can also control the look of a form by setting values to the appropriate property. For example, you can change the color of the text in a label control by changing the value of the ForeColor property.
Private Sub CommandButton1_Click()
lblMyText.ForeColor = &HFF0000 //blue
End Sub
---------------------------------------------------------------
Private Sub CommandButton2_Click()
lblMyText.ForeColor = &HFF& //red
End Sub