
Tips.Net > WordTips Home > Fields > Cross-References > Controlling the Format of Cross-References
Summary: Cross references to captions can be very handy in certain types of documents. Word displays cross references to captions in the format that they originally appear. If you want to modify that format, this tip explains how to go about it. (This tip works with Microsoft Word 97, Word 2000, Word 2002, and Word 2003.)
Stephen Weiner asked if it is possible to control the format of cross-references inserted by Word. When he inserts a label and number such as Table 1 or Figure 12, he wants the label lowercase (table, figure) and a non-breaking space between the label and the number.
There is no way to control this type of cross-reference formatting in Word. Obviously you can change the cross-references manually after placing them, but whenever you update fields the original Word-chosen format will be used for them. There are a couple of macro-based solutions you can try. The first solution will change the actual field codes used for the field:
Sub FieldRefChanges1()
On Error Resume Next
Dim oStoryRng As Range
Dim oFld As Field
For Each oStoryRng In ActiveDocument.StoryRanges
For Each oFld In oStoryRng.Fields
If oFld.Type = wdFieldRef And oFld.Result.Words.Count <= 2 Then
'add format switch with lowercase option to field codes
oFld.Code.Text = oFld.Code.Text & "\* lower "
'updates the field results to display the new format
oFld.Update
End If
Next oFld
Next oStoryRng
End Sub
The macro includes a couple of nested For loops. The first one steps through each story in the document, and the second goes through each field in each story. An If statement is then used to make sure that the field is a REF field (the kind used for cross-references), and that the result of the field is two or fewer words (as in Table 1 or Figure 12).
If these criteria are met, then the macro makes a change to the actual field code, adding the switch that results in the field being displayed in lowercase.
There are a couple of drawbacks to this macro. First, if you run it multiple times, the \* lower switch is added to the REF fields multiple times. Second, the macro doesn't change the space in the field results to a non-breaking space.
To overcome both problems, just modify the macro so that it automates the manual process you would go through to change the macro results.
Sub FieldRefChanges2()
On Error Resume Next
Dim oStoryRng As Range
Dim oFld As Field
Dim sTemp As String
Dim J As String
For Each oStoryRng In ActiveDocument.StoryRanges
For Each oFld In oStoryRng.Fields
If oFld.Type = wdFieldRef And oFld.Result.Words.Count <= 2 Then
sTemp = oFld.Result.Text
sTemp = LCase(sTemp)
J = InStr(sTemp, " ")
sTemp = Left(sTemp, J - 1) & Chr(160) & _
Mid(sTemp, J + 1, Len(sTemp) - J)
oFld.Result.Text = sTemp
End If
Next oFld
Next oStoryRng
End Sub
This macro is essentially the same as the previous one, except that it works strictly with the result text for the field. The text is assigned to the sTemp variable, which is then converted to lowercase. The position of the space is determined, and it is replaced with a non-breaking space. The result is then stuffed back into the result text for the field.
Tip #141 applies to Microsoft Word versions: 97 2000 2002 2003
Ultimate Library! An amazing resource that brings together, in one place, the collected knowledge of everything ever published in WordTips. The library combines two powerful elements to make you more productive: solutions and convenience. Here's where you get your own copy of everything ever published in WordTips.
Check out WordTips Ten-Year Library today!
Word includes two powerful search engines. Do you know how to use them both? You can discover new knowledge with WordTips: Find and Replace. (more information...)
Ask a Word Question
Make a Comment
Beauty Tips
Bugs and Pests Tips
Car Tips
Cleaning Tips
College Tips
Cooking Tips
Excel2007 Tips
ExcelTips
Family Tips
Gardening Tips
Health Tips
Home Tips
Money Tips
Organizing Tips
Pet Tips
Word2007 Tips
WordTips