A Simple Macro

A good way to get started with WordBasic is to look at a simple macro. This one transposes characters (for example, to change "teh" to "the"). The following table illustrates the actions that the macro takes and the corresponding WordBasic instructions.

What happens
on the screen

WordBasic
instruction


Explanation

Before the macro begins, the insertion point is positioned after the letters to be switched.

CharLeft 1, 1

First, the macro moves the insertion point to the left by one character and selects that character.

EditCut

Then it cuts the selected letter.

CharLeft 1

Then it moves the insertion point to the left by one character.

EditPaste

Next, it pastes the character that was cut.

CharRight 1

Finally, it moves the insertion point to the right by one character — back to where it was before the macro began.


You can easily record this macro by starting the macro recorder and performing the actions described in the table. (Remember, while recording a macro, you need to use the keyboard to move the insertion point and select text.) If you do so and then choose the Macro command (Tools menu) to open the macro in a macro-editing window, you'll see the following instructions:


Sub MAIN
CharLeft 1, 1
EditCut
CharLeft 1
EditPaste
CharRight 1
End Sub

As explained in the previous chapter, Sub MAIN and End Sub begin and end every macro. Notice that each instruction within the macro is on a separate line. You can place more than one instruction on the same line, but only if you separate each instruction with a colon (:). Generally, macros are easier to read if each instruction is on a separate line.

Also, notice the pattern of capitalization. When Word saves a macro, it gives the proper capitalization to every word that it recognizes as part of the WordBasic "vocabulary." You do not have to worry about typing instructions with the correct capitalization because WordBasic is not case-sensitive — it recognizes instructions typed in any combination of uppercase and lowercase letters.

Two of the instructions, CharLeft and CharRight, move the insertion point and optionally select text. The other two instructions, EditCut and EditPaste, correspond to the Cut and Paste commands on the Edit menu. In Word, virtually every action you can take has an equivalent WordBasic instruction.

Comments

Comments explain a macro to others and remind you of the purpose of the macro or of particular instructions. Word ignores comments when it runs the macro.

You can use the REM statement to insert a comment in a macro. For example:

REM This is a comment

Or you can use an apostrophe:

'This is also a comment

Both ways of commenting have advantages. The REM statement helps to make comments visible. But REM and the comment that follows it must be on their own line. You can use an apostrophe to place a comment on the same line as an instruction. Word interprets any text after the apostrophe until the end of the line as a comment. For example, you could add comments to these instructions from the macro that transposes characters described earlier in this chapter:

CharLeft 1, 1 'Select character to the left

EditCut 'Cut selected character

You can use the Add/Remove REM button on the Macro toolbar to easily add or remove REM statements. For more information about using the Add/Remove REM button, see Chapter 6, "Debugging."