A variable is a storage container for a string or a number. A macro can change the contents of a variable as it is running. In other words, the value of a variable — the string or number it contains — can vary, which is why a variable is so named.
Variables provide the means for flexible and powerful macros. For example, to avoid overusing the word "cool," you could write a macro to calculate how many times the word appears in your document or in a paragraph. This macro may be useful, but it is not very flexible because it counts only the word "cool." Using a variable, you can modify the macro to calculate how frequently any word that you specify appears in a document. For a discussion of this macro, see "Some Sample Macros" at the end of this chapter.
WordBasic supports string variables and numeric variables to store strings and numbers. A string variable is identified by a dollar sign ($) ending character. A numeric variable does not end with a dollar sign character. Here are some examples of possible string and numeric variable names.
Examples of | Examples of | |
MyName$ | Total | |
SearchText$ | count | |
answer$ | size |
When creating a variable name, keep the following rules in mind:
A variable has no value until you assign it one. In effect, it is an empty container waiting to be filled by a string or numeric value.
In some programming languages, you have to "declare" variables before you can assign values to them. In other words, before you write the body of your program, you must specify all the words that are going to be used as variable names. You do not have to declare variables in WordBasic, so often a variable appears in your macro for the first time when you assign it a value. But for complex macros that use many variables, you can make the macro more readable by declaring all the variables at the beginning of the macro. You use the Dim statement to declare a variable. For information, see Dim in Part 2, "WordBasic Reference."
To assign a value to a variable, you use an equal sign (=), placing the variable name on the left side and the value you are assigning it on the right side. The following example assigns the string "Willie" to the variable MyName$:
MyName$ = "Willie"
Don't try to place the value you are assigning on the left side of the equal sign — this doesn't work. The following example produces a syntax error:
"Willie" = MyName$ 'Produces a syntax error
Once you have assigned a value to a variable, you can use that variable just as you would use a string or number. If the variable is numeric, you can use it in mathematical expressions. The first line of the following example assigns to counter a value of 6. The second line assigns to counter the result of the expression counter + 1, or 7:
counter = 6 counter = counter + 1
You cannot assign numeric values to string variables or string values to numeric variables. The following statements are not acceptable (either statement would prevent a macro from running and cause Word to display the message "Type mismatch"):
strg$ = 3.14159 'Produces an error number = "hi, there" 'Produces an error
Variables often act as containers for information returned by a function. For example:
firstdoc$ = FileName$()
In this example, firstdoc$ stores the filename of the current document.
Sometimes you need to change a string value to a numeric value, or vice versa. You use the Val() function to convert a string to a number and the Str$() function to convert a number to a string. For example, the following input box asks the user to type the number of files the macro should open.
The InputBox$() function, which displays the input box, returns a string value. So the Val() function is used to convert the string value to the NumFiles numeric value:
NumFilesString$ = InputBox$("How many files do you want to list?") NumFiles = Val(NumFilesString$)
On the other hand, the MsgBox statement accepts only string values. So if you want to use MsgBox to display a numeric value, you must first convert it into a string with the Str$() function. For example:
NumFiles$ = Str$(NumFiles) MsgBox "You have chosen to list" + NumFiles$ + " files."
These instructions display the following message box.
The Str$() function adds a space before positive numbers. The space accommodates the minus sign (–) for negative numbers. You can use the LTrim$() function to remove the space. For an example, see LTrim$() in Part 2, "WordBasic Reference." For more information on InputBox$() and MsgBox, see "Displaying Messages and Requesting Information" later in this chapter.