Hack 22 Make More Flexible CaptionsWord 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 Insert
Figure 3-36. Creating a caption using a STYLEREF fieldClick the OK button to insert the field in your document. If
you've turned on the option to make field codes
always visible (Tools Figure 3-37. A STYLEREF field, one of the building blocks for a captionImmediately after the field, put in a hyphen. Then select
Insert Figure 3-38. Adding a SEQ field to a caption to increment the numberingWith these fields in the document, your caption should now look like Figure 3-39. The figure shows two captions: the first shows the field results and the second shows the field codes. Figure 3-39. 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 3-40. In this case, the captions are set to get the chapter number from the Heading 1 style. Figure 3-40. Word's caption feature also uses a combination of STYLEREF and SEQ fields3.10.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", _ 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. |