Articles  
 
[Printer-friendly version]
Using variables in your macros
By Gordon McComb

Remember studying about variables in Mr. Hogbottom’s algebra class? Everybody in the class except you seemed to understand what variables were all about. So it’s probably not great news to you that WordPerfect® macros have variables, too.

Relax! WordPerfect macro variables are a lot easier to understand than those silly things in algebra. Read all about macro variables here—what they are, and how to use them.

And even better news: unlike in Old Hogbottom’s class, there won’t be a pop quiz afterward. (The discussion that follows applies to WordPerfect 6.1 for Windows® and later.)

Understanding what a variable is
Think of a variable as an envelope. You use the envelope to store pieces of information that your macro will use later on. Inside the envelope goes the data you want to keep; on the outside of the envelope you write a short description—a name—of what’s inside the envelope.

That way, as you collect more variable “envelopes,” you can sort through the pile and quickly pick out the one you want.

Of course, in the real world of computers, there are no envelopes involved in creating variables. More precisely, a variable is a block of memory inside your computer.

WordPerfect sets aside this memory and stores the variable data in there. It’s called a “variable” because the information can change each time the macro is played.

For example, suppose the variable holds a postal (Zip) code. The first time the macro is played, the variable might contains the postal code 12345. The next time the macro is played, the variable contains 98765, and so forth. Any data that changes each time the macro runs is a natural candidate for storage in a variable.

Variables are also quite handy when data needs to repeated many times throughout a macro. Instead of repeating the same data each time, you just store it once in a variable, then have the macro access the variable whenever you want to use the data.

Creating and using a variable
To use a variable you must first create it. Once created you can then use (“refer”) to the variable elsewhere in the macro.

Step 1: Store the data into the variable. This step can be done either when the macro is written, or when the macro is run. A variable stored when the macro is written never changes.

This technique is used when you want to repeat the contents of the variable many times throughout a macro. A variable stored when the macro is played allows you to change the contents of the variable each time the macro runs.

Step 2: Retrieve the data from the variable. There are many ways of retrieving or using the data in a variable.

The most common is to insert the contents of the variable using the Type command. Instead of specifying a text string for the Type command you use the name of the variable that contains the data that you want.

The process of storing a value in a variable is more technically described as “assigning a variable”. Whenever you assign a variable, you must think of a name for the variable. WordPerfect is pretty flexible when it comes to variable names, but there are a few rules:

  • Variable names must be 50 characters or less. The shorter the variable name the better, because it means less typing for you
  • Variable names cannot contain a space character or most punctuation characters (such as $ and *). The notable exception is the underscore, which can be used to create variable names with “pseudo spaces” such as My_Variable
  • Variables names cannot begin with a number character, such as 1Variable. However, a number character can be used anywhere else in the variable name
  • Variable names should not be the same as the names of WordPerfect macro commands. For example, avoid naming your variable Type, as this is already the name of a macro command



  • GetString: An Example of Using Variables
    The GetString command is a good way to demonstrate the use of macro variables. GetString is used to display a dialog box which asks the user to type some text. At the top of the GetString dialog is a caption, which you can use to provide the name of your macro, or even more explanatory text.

    For basic use, the GetString command uses three parameters

  • The name of the variable you want to use to hold the text the user types in
  • The explanatory text that explains what to do—called the “message”
  • The caption, called the “title”, at the top of the GetString dialog box


  • Here’s the syntax of the GetString command, with the parameters in their expected order:

    GetString (VariableName; Message; Title)

    To use the GetString command, simply replace VariableName with the name of the variable you want to use. And, replace Message and Title with the message and title you want to use. Here’s an example (note the quotes around the Message and Title, but not around Answer):

    GetString (Answer; "Type your name, then choose OK"; "GetString Example")

    To see how this all works, try it for yourself by opening a new, blank document. Display the Macro Feature Bar by choosing Tools, Macro, Macro Bar. Type the above line exactly as you see it. When done, click the Save & Compile button (it’s in the Macro Feature Bar).

    Name the macro TEMP.WCM, and close it when done. To play it, press ALT + F10, type TEMP, and choose Play.

    The GetString dialog box appears. Type some text into the entry box, then choose OK. The macro ends, but nothing else happens, because you haven’t told WordPerfect what to do with the information stored in the Answer variable. That comes in the next section.

    Retrieving the GetString Variable
    You obviously want to retrieve the data collected by the GetString command and stored in the Answer variable. That can readily be done with the Type command, like this:

    Type (Answer)

    Note that there are no quotes around Answer.

    Test the macro by retrieving TEMP.WCM, and add the Type (Answer) command on the line below the GetString command. Your macro should look like the following:

    GetString (Answer; "Type your name, then choose OK"; "GetString Example")
    Type (Answer)


    Save and close the TEMP.WCM Macro. Play the macro in the usual way. At the GetString dialog box, type your name, and choose OK. This places your name into the Answer variable. WordPerfect then types your name, previously stored in the Answer variable.

    You’re always free to embellish the action of the macro. The YOURNAME.WCM macro, shown below, combines the GetString command with two Type commands. Type your name at the GetString dialog box, and the macro responds by typing Hello, then your name.

    For example, if you type George Jetson at the GetString dialog box, the macro types “Hello, George Jetson”.

    GetString (Answer; "Type your name, then choose OK"; "GetString Example")
    Type ("Hello, ")
    Type (Answer)


    Using Variables To Collect Lots of Data
    Quite often, the macros you develop will use lots of variables. There is no rule that says you can't ask the user for a string of information, and store each piece in a different variable. WordPerfect can accommodate an almost unlimited number of variables; the actual number depends on the amount of memory in your computer. As a rough estimate, you can assign hundreds of variables in a macro before risking running out of available memory.

    In the following macro, named BARLABEL.WCM, a series of five GetString commands collect the user's name and address. Each GetString command uses a different variable name. These names are: Name, StreetAddress, City, State, and Zip. The variable names were chosen to represent their contents. Obviously, City holds the city, State holds the state, and so forth.


    GetString (Name; "Name")
    GetString (StreetAddress; "Street Address ")
    GetString (City; "City")
    String (State; "State")
    GetString (Zip; Postal Code")
    Type (Name)
    HardReturn()
    Type (StreetAddress)
    HardReturn()
    Type (City)
    Type (", ")
    Type (State)
    Type (" ")
    Type (Zip)
    HardReturn()
    BarCodePOSTNET (Zip)


    Create this macro in a new, blank document, and turn the Macro Feature Bar on by choosing Tools, Macro, Macro Bar. Type the macro exactly as it appears above. Save the macro as BARLABEL.WCM, and close the document file.

    In a blank document, play the macro by pressing ALT + F10, typing BARLABEL, and choosing Play. At each GetString dialog box that appears, type the information requested, and choose OK. After collecting the Zip code information, the macro types out the data previously stored in the variables.

    See how the macro uses the Zip variable twice: once to type the Zip code as text, and once again to create a postal bar code. The BarCodePOSTNET command converts a valid US Zip code into a properly formed postal bar code.

    Assigning Variables Within a Macro
    So far you've read about assigning variables when a macro is played. This allows you to interact with the macro, and therefore have more control over its operation. You can also assign variables within a macro, while you are writing it.

    Once assigned, the variable can be used as usual. You can include the variable as part of a Type command, for example, or use it with any of the hundreds of other WordPerfect macro commands that use a parameter.

    To assign a variable within a macro, you create something called a variable assignment expression, which is as simple as:

    MyVar="This is the contents of the variable"

    The variable assignment expression is composed of three parts: the name of the variable, the equals sign, and the contents.

    What is the purpose of assigning a variable in this way? There are lots of reasons, and they depend mostly on how the variable will be used. One of the most common reasons is to repeat the same text over and over again.

    For example, you can assign your name to a variable called Name, and then use that variable many times in a macro. Instead of repeating the text of your name each time, you just use the Name variable instead.

    Another reason to use variable assignments is to create customized macros. For example, the following short macro creates a memo, and automatically inserts your name. You can create multiple versions of this macro for other people.

    To change the name that appears in the memo, just change the assignment for the Name variable:

    Name="Put Your Name Here"
    FontSize (18.0p)
    AttributeAppearanceToggle (Bold!)
    Center ()
    Type ("INTEROFFICE MEMO")
    AttributeAppearanceToggle (Bold!)
    HardReturn ()
    HardReturn ()
    FontSize (12.0p)
    Type ("Date: ")
    DateText ()
    HardReturn ()
    HardReturn ()
    Type ("From: " + Name)
    HardReturn ()
    HardReturn ()
    Type ("To: ")
    PauseKey (Enter!)
    HardReturn ()
    HardReturn ()
    Type ("Subject: ")
    PauseKey (Enter!)
    HardReturn ()
    HardReturn ()


    If you'd like to try this macro, retype it in a new, blank document and choose Tools, Macro, Macro Bar to display the Macro Feature Bar. Change the variable assignment in the first line to your name. Be sure to enclose your name in double-quotes, as shown, or WordPerfect will report an error. Save the macro as MYMEMO.WCM. Close the MYMEMO.WCM document file.

    Play the macro in a blank document window by pressing ALT + F10, typing MYMEMO, and pressing Play. The macro automatically inserts the data and your name in the From: line, and prompts you for the name of the person you are sending the memo to, as well as the subject of the memo.

    You see...macro variables aren't at all complicated—and they're super-useful, too. If only Mr. Hogbottom could have explained algebraic variables in one short article, your life would be so different today. At least you wouldn't have had to spend so many hours in detention for throwing spit wads at the teacher.

    Gordon McComb is a writer and consultant specializing in macros for word processors. His latest books on WordPerfect macros can be found at http://www.gmccomb.com. He can be reached at gmccomb@gmccomb.com.

    Disclaimer: The information provided on OfficeCommunity.com is not legal advice, but is intended to be general

       
      WordPerfect®
      Corel™ Presentations®
      Quattro® Pro
      CorelCENTRAL™
      Paradox®
     
     



    Copyright © 2003 Corel Corporation. All rights reserved.
    Company and product names are trademarks or registered trademarks of their respective companies.