Advanced Macros in Word

Some Word users may know how to record an often-repeated series of actions, text, and manipulations and repeat them automatically with one key combination: a Word macro. But without realizing it, they start working with a very powerful functionality that lies behind every module of Microsoft Office: Visual Basic for Applications, VBA for short. In this way the door opens to a world of gadgets, tricks and other ingenuity that you can develop yourself.

Via menu or keyboard

In Word 2003/2007 press Alt+F11 and we enter the VBA editor. This is also possible via the Word 2007 Ribbon (Developers tab / Visual Basic button) or the menu in Word 2003 and earlier (choose Tools / Macro / Visual Basic Editor ). With the same Alt+F11 we switch back and forth between Word and the VBA editor. To run a previously recorded macro or self-designed VBA program, press Alt+F8, or in Word 2007 click the Macros button on the Developer tab of the Ribbon (or in Word 2003: Tools / Macros / Macros ) . Starting a macro recording can then again be done via the Record macro button (in Word 2003 and earlier: Tools / Macro / Record new macro ), but is much faster if we double-click on the window icon (in Word 2003 and earlier: the OPN box).

Recording an operation as a macro is the easiest way to produce VBA code.

Not just in Word

Virtually all the basic skills described in this Expert course also apply to any other module of Microsoft Office: just try things out in Excel, PowerPoint, Outlook, Access, Project or Visio. In fact, there is a lot of third-party software that has also chosen to offer VBA as a macro language, complete with the VBA editor and all the listed keyboard shortcuts, including AutoCAD, iGrafx FlowCharter, CorelDRAW, WordPerfect Office, BusinessObjects, etc.

The first steps

The easiest way to get into the world of VBA is to macro-record an operation, which you then extend and refine by coding additional functionality. For example, suppose you decide in an existing document to have every sentence within a block of text followed by two spaces instead of one. A first approach for this is to repeatedly call up the search/replace function (see also the Expert course 'Find and replace' in the previous issue of Computer!Totaal): search for a period followed by a space and replace it with a period with two spaces. Then start over for the question mark, exclamation mark, etc. This should be faster and more elegant, right? On to VBA!

The VBA editor

Let's include that first replacement stroke in a macro for which we accept the default suggested name Macro1 and indicate that we want to store it in the current Document1 . Then we press Alt+F11, and we end up in the VBA editor. From here on we have to accept a bilingual world: the user interface remains in Dutch (or in whatever language Word is used), but the code is always in English, even if it serves to automate a Dutch Word. The main workspaces within the VBA editor are the Code window and the Project Explorer . The latter shows all active documents and templates in a tree structure. We expand Project (Document1), and we see three branches: Microsoft Word objects, Modules and References , each with one child level. Under Modules, it's NewMacros . Finally, when we double click on NewMacros, the code we (unknowingly) generated will appear in the code window

Alt+F11 brings us to the VBA editor from every module of Microsoft Office.

Structure

A standalone piece of software is called a subroutine in VBA. Hence any code it contains between the instructions Sub and End Sub stands. the novice Sub is followed by a custom name for the routine. If it concerns a recorded macro, this is the name chosen in the Word dialog before the recording started. But this can still be changed in the editor. The name you use is shown in the list of available macros that you open with Alt+F8.

Instructions and declarations can come between these two lines, in principle one per line, unless they are separated by colons. A line beginning with an apostrophe (') indicates a comment line. Such pieces of text have no influence on the course of the program: they are skipped and therefore only serve as documentation. They are automatically set to green by the VBA editor. Reserved keywords come in blue, the rest of the text in black, lines with errors in red. A line of source code is closed by pressing Enter: do not use periods or other punctuation marks, unless the code (for reasons of readability, for example) continues on the next line: then we end the line with a space and an 'underscore' ( the flat hyphen: _).

Declarations are pre-announcements of variables we intend to use, along with their type. Such notifications start with the keyword dim, followed by a name of your choice for the variable, then Ash and the type (see box). They are optional, unless Declare Variables Required is checked in the Editor tab under Tools / Options , or if we specify at the beginning of the code (i.e. before the first Subline) the instruction Place option Explicit.

In the options of the VBA editor we indicate whether we want variables declared explicitly.

Recent Posts

$config[zx-auto] not found$config[zx-overlay] not found