Customization and macros

Customize dialogIn Word, most of the interface is fully customizable. That means that you can assign any command you want to a keyboard shortcut (i.e. a Ctrl- or Alt-key combination), a menu item, or a button on a toolbar. Moreover any of these items can do more than invoke built-in commands: they can be assigned to

These options are available through the Tools | Customize | Commands dialog box; you'll find them at the very bottom of the “categories” list, under “All Commands”.

CustomToolbarTo add an item to a toolbar or a menu, just drag it from the “commands” list to where you want it. If you make several new buttons, you might want to put them on a new toolbar or menu: this facilitates copying them to another document or template with the organizer. (At left is a custom toolbar of useful commands and macros for writing papers.)

Button EditorBy default items which aren't built-in commands will display as text buttons. If you want a button image, right-click the button while the customize dialog is still open; you will see options for modifying the button's display. You can copy an image from another button, choose from a set of pre-defined images, or make your own image in the button editor. See online help under “custom_toolbar” for more information.

There seems to be a bug in Word 2000 which results in the background color of a handmade button displaying incorrectly on a monitor with different settings. You can avoid this by hand-coloring all the pixels in the editor.

Recording a macro

A “macro” (or “script”) is a small computer program used to control another program. The simplest kind just stores a series of commands and executes them in order. Current versions of Microsoft Office programs, however, use Visual Basic for Applications (VBA) as their macro language; this is a full-fledged programming language in which you can do just about anything you want. Thus, macros in Word can be as simple or elaborate as you like.

The easiest way to write a macro is to record one. Word can keep track of commands as you execute them so you can repeat the same series later. There are a couple of limitations on this process to be aware of:

Record macro dialogClick Tools | Macro | Record New Macro (or double-click the gray “REC” on the status bar). You will see a dialog which lets you enter information about the macro. The interesting options:

A small window comes up with “pause” and “stop” buttons. Give your series of commands and keystrokes, and then click “stop”. That's it!

Editing a macro

If you want to modify a macro you've recorded, it may be easier to edit the code than to re-record a different set of keystrokes. Choose Tools | Macro | Macros, select the one you want to modify and click Edit. You'll see the Visual Basic (VB) editor. The details of VB are beyond the scope of this document, but it's not too difficult to pick up enough from examining recorded macros and cutting and pasting between them to do some fairly sophisticated things.

If you're interested in editing macros, make sure you've installed the Visual Basic help files with Word; they aren't part of the default installation. When they're installed, you can get information about any VB command by positioning the cursor on it in the VB editor and pressing F1.

Some sample macros

Below are some macros to give you an idea of what's involved. You can add these macros directly to your copy of Word. To add them to a document or template:

  1. Select the macro text in your browser and copy it to the clipboard (Ctrl-C).
  2. In Word, open the document or template where you want to save the macro.
  3. Choose Tools | Macro | Macros | Create. You'll see the Visual Basic editor.
  4. If there are already some macros in the document, move to the end of the file. Paste the macro (Ctrl-V). If you want you can insert it into a new “module” instead: choose Insert | Module to get a blank file, and paste the macro there.
  5. Note that line breaks are important in VB. If you need to break a line of code so you can read the whole thing, make sure there's an underscore (_) at the end of the first part of the line.
  6. To test the new macro, place the cursor inside it and type F5 or click the “play” button. The macro will execute in your open Word document.

In addition to these, I've made several more macros and a corresponding toolbar available in a Word template, Lingword.dot.

ExampleNumber: A simple recorded keystroke macro that inserts an example number (i.e. a SEQ field) between parentheses at the cursor, and adds a tab afterwards. If you don't want the tab, delete “& vbTab” at the end.

Sub ExampleNumber()
'
' ExampleNumber Macro
' by Susanna Cumming from "Word For Linguists"
'
Selection.TypeText Text:="("
Selection.Fields.Add Range:=Selection.Range, _
  Type:=wdFieldEmpty, Text:="SEQ EX"
Selection.TypeText Text:=")" & vbTab
End Sub

PasteAsPicture: Here's a slightly more complex example with some conditional logic. It pastes a graphic into a Word document as a metafile object, formats it as an inline graphic, and centers it. (This procedure is useful for inserting Excel graphs in a memory-friendly way). This behavior couldn't be reproduced by recording keystrokes, so the macro was written directly in VBA.

Sub PasteAsPicture()
'
' PasteAsPicture Macro
' by Susanna Cumming from "Word For Linguists"
'
Selection.PasteSpecial DataType:=wdPasteEnhancedMetafile
With Selection.ShapeRange(1)
  If .Type = msoPicture Then .ConvertToInlineShape
End With
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
End Sub