Make More Flexible Captions

Make More Flexible Captions

figs/moderate.gif figs/hack22.gif

Word offers a built-in captions feature, but it allows you to use a heading style only for the chapter number. This hack shows you how to expand your options.

If you ask Word to include the chapter number in a caption, you must specify the heading level. But what if you use a style other than one of the built-in heading styles to number your chapters? By using two kinds of fields, you can have your captions use any style you like as the base for the chapter number. The following example shows you how to create a figure caption that gets its chapter number from a custom paragraph style named "ChapterLabel."

Put your cursor where you want to place the caption. Next, type the word "Figure," followed by a space. Select InsertField and insert a STYLEREF field pointing to the ChapterLabel paragraph style, as shown in Figure.

The ChapterLabel paragraph style (or any other style you specify) must exist within the document for this hack to work, and it must also actually be in use within the document.

Creating a caption using a STYLEREF field

Click the OK button to insert the field in your document. If you've turned on the option to make field codes always visible (ToolsOptionsView), the field will look like Figure. If you don't see the field code, select the text you just inserted, then right-click and choose Toggle Field Codes.

A STYLEREF field, one of the building blocks for a caption

Immediately after the field, put in a hyphen. Then select InsertField and insert a SEQ field. Use "Figure" as the identifier, as shown in Figure.

Adding a SEQ field to a caption to increment the numbering

With these fields in the document, your caption should now look like Figure. The figure shows two captions: the first shows the field results and the second shows the field codes.

The field results (top) and the field codes used to produce the results (bottom)

Finish the caption label with a period, and you can now type the caption text.

In the next section, you will learn how to create captions like this with a macro, but in a pinch you can always copy and paste to create additional captions.

Though the syntax used is slightly different, Word's built-in captions feature also uses a combination of STYLEREF and SEQ fields, as shown in Figure. In this case, the captions are set to get the chapter number from the Heading 1 style.

Word's caption feature also uses a combination of STYLEREF and SEQ fields

1 Automating the Captions

Obviously, a macro would help you insert captions much faster. The following example comes from the macros used to insert the captions for this book:

Sub InsertFigureCaption( )

Dim bIsParagraphEmpty As Boolean

With Selection

    .Expand wdParagraph

    If .Characters.Count = 1 Then bIsParagraphEmpty = True

    .Collapse wdCollapseStart

    .Style = "Caption"

    .InsertBefore "Figure "

    .Collapse wdCollapseEnd

    .Fields.Add _

        Range:=Selection.Range, _

        Type:=wdFieldStyleRef, _

        Text:="ChapterLabel", _


    .Collapse wdCollapseEnd

    .InsertAfter "-"

    .Collapse wdCollapseEnd

    .Fields.Add _

        Range:=Selection.Range, _

        Type:=wdFieldSequence, _

        Text:="Figure", _


    .InsertAfter ". "

    .Collapse wdCollapseEnd

    If bIsParagraphEmpty = True Then

        .InsertAfter "Caption Text Goes Here"


        .Expand wdParagraph

    End If

End With

End Sub

If the paragraph already contains text when you run this macro, it prefaces the text with a caption label. If no text exists, it inserts some dummy text for you to replace later.

     Python   SQL   Java   php   Perl 
     game development   web development   internet   *nix   graphics   hardware 
     telecommunications   C++ 
     Flash   Active Directory   Windows