March 26, 2011, 3:59 a.m.
posted by francis
Make More Flexible Captions
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.
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.
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
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", _ PreserveFormatting:=True .Collapse wdCollapseEnd .InsertAfter "-" .Collapse wdCollapseEnd .Fields.Add _ Range:=Selection.Range, _ Type:=wdFieldSequence, _ Text:="Figure", _ PreserveFormatting:=True .InsertAfter ". " .Collapse wdCollapseEnd If bIsParagraphEmpty = True Then .InsertAfter "Caption Text Goes Here" Else .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.