Making the transition from WordBasic to VBA

Article contributed by Bill Coan

The most difficult part about making the transition from WordBasic to VBA is learning to use Word's object model (instead of Word's user interface) as an organizing principle.

As a WordBasic programmer, you could view macros as silent, behind-the-scenes users of Word. Indeed, if you read through a WordBasic macro, you find a series of commands that might just as well have come from a user. When Word 6 or 7 executed a macro, it behaved exactly as it would have behaved if a user were sitting at the keyboard and executing the commands manually.

Sure, WordBasic offered looping and branching, but these were merely tools for managing the order in which commands were to be executed. The commands themselves might just as well have come from a user.

As a Visual Basic programmer, you can view your macros as object manipulators. You can drill down to a particular object and then manipulate it. In the end, the types of manipulations that you can perform aren't all that different from what the user can accomplish via the user interface. But your macros can reach objects in more powerful and flexible ways than user interface commands could ever do. Moreover, your macros can reach those objects without regard to the current position of the selection. Plus, you can branch and loop in new, more powerful ways than you could with WordBasic.

Consider the following statement, which allows you to manipulate the entire range of text found in the second row of the first table of the active document:

ActiveDocument.Tables(1).Rows(2).Range.Bold = True

Now consider this looping structure, which allows you to manipulate the same range within all tables in the document:

Dim oTable As Table
For Each oTable In ActiveDocument.Tables
  oTable.Rows(2).Range.Bold = True
Next oTable

Powerful stuff, eh??