Is it possible to assign a specific style to all cross-references in Word 2007?

35

19

Nothing more to add, I would like to change the style of all cross-references I have in a Word 2007 document at once. But I have no idea how to do it. How can this be done?

Drake

Posted 2009-07-27T07:42:58.060

Reputation: 5 104

Good question, I couldn't find anything quick and dirty. But I think it's related to the style of the entire document – Ivo Flipse – 2009-07-27T08:22:48.557

1@Ivo, yes I thinks too that is something related to the style of the document, but I am a poor SW developer with no much experience with Word :) – Drake – 2009-07-27T08:55:56.337

@marco: see my updated answer – Paused until further notice. – 2009-07-27T23:27:45.467

yeah, great answer thanks a lot, very very interesting – Drake – 2009-07-28T09:03:19.183

Answers

41

Some cross-reference types are automatically formatted with the "intense reference" style, but most are formatted as "normal" text.

To apply the "intense reference" style to the text of the cross reference:

  • select the text
  • choose the "Home" tab in the ribbon
  • using either the up-down buttons or the drop-down button in the "Styles" group of the ribbon, choose the "intense reference" style (or another style if you prefer)

To change the appearance of all text of a given style:

  • choose the "Home" tab in the ribbon
  • using the drop-down button in the "Styles" group of the ribbon, choose "Apply styles..."
  • in the "Apply Styles" dialog box under "Style Name" choose the name of the style you want to change (e.g. "intense reference")
  • Click the "Modify..." button
  • Change the formatting to suit you and click "OK"

To apply a style to all cross references at once:

  • Press Alt+F9 to show field codes
  • Select the "Home" tab in the ribbon
  • Click on "Replace" in the "Editing" group
  • In the "Find what" field, type ^19 REF
    • (That's caret-one-nine-space-R-E-F)
  • Click in the "Replace with" field, but don't type anything
  • Click the "More" button
  • The bottom section of the dialog should be titled "Replace" (with a horizontal rule after it)
  • Click the "Format" button and select "Style..."
  • Choose a style (e.g. "Intense Reference") and click OK
  • It should now show the style you selected under the "Replace with" field
  • Click "Replace All" if you're feeling brave or use "Find Next" and "Replace" to step through and replace or skip each reference field code's style individually
  • Press Alt+F9 to hide field codes

See this page for more information on special codes in Find and Replace.

Here is a macro that will add the switch \* mergeformat to each of the fields. This switch is necessary to keep the formatting from being lost if you do a field update. You can assign the macro to a keystroke and it will step through the fields one at a time for each time you press the keystroke. You can also edit the macro to loop over the whole document to automate the process.

Sub mf()
'
' mf Macro
' Find cross references and add \* mergeformat
'
    Selection.Find.ClearFormatting
    With Selection.Find
        .Text = "^19 REF"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute
    Selection.MoveRight Unit:=wdCharacter, Count:=1
    Selection.MoveLeft Unit:=wdCharacter, Count:=1
    Selection.TypeText Text:="\* mergeformat "
    Selection.Find.Execute
End Sub

Paused until further notice.

Posted 2009-07-27T07:42:58.060

Reputation: 86 075

5Jesus Christ... this works great but the thing is: why make it so difficult to apply a style to Cross-References!? ;) – Leniel Maccaferri – 2014-11-14T18:34:03.840

in fact most references if not all are create by default with normal text. Thank you for the answer, but in this case I have to set manually the style of each one before I can update the style for all at once. I was searching for a method that can automatize the first step of the process – Drake – 2009-07-27T12:58:44.693

6wow, i didn't know this advanced uses of find/replace, very helpful – Drake – 2009-07-28T09:04:29.243

on MS Word 16.9.1 on Mac, this works (didn't try the macro), but the find/replace is not on the home ribbon (I think?), but instead in the menu under Edit->Find->Advanced Find and Replace... – Michael – 2018-01-31T02:39:01.167

1/ If it doesn't work check if you did not confuse Intense Reference with Intense Emphasis 2/ For the macro that loop through the whole document and apply the (new) style to all cross-ref see "Jaykrushna patel" answer below – JinSnow – 2018-09-26T15:46:28.980

@Dennis it works for the heading text but the style doesn't apply to the page number, any idea how to do that? – JinSnow – 2018-09-26T17:20:36.417

@JinSnow: Sorry, no. – Paused until further notice. – 2018-09-26T19:20:18.250

This is brilliant. Just what I was looking for. Works like a charm in Word (MS Office 365 Business) version 1812 – David Brossard – 2019-01-24T21:36:37.933

PS: if I save as PDF, it loses the formatting – David Brossard – 2019-01-24T21:42:02.040

@DavidBrossard: What happens if you print to PDF instead of save as PDF? – Paused until further notice. – 2019-01-24T21:47:22.133

I thought of that :-) The same sadly... – David Brossard – 2019-01-24T22:42:13.437

6You answer is a little gem of condensed knowledge. Worth its own wiki! – Grimace of Despair – 2013-07-18T09:23:19.353

5

  • Press Alt+F9 to show field codes
  • Use the following macro to add CHARFORMAT to all cross references. This macro adds the string to the field only if it's not already there.

    Sub SetCHARFORMAT()
    '
    ' Set CHARFORMAT switch to all {REF} fields. Replace MERGEFORMAT.
    '
    '
        Dim oField As Field
        Dim oRng As Range
        For Each oField In ActiveDocument.Fields
        'For Each oField In Selection.Fields
            If InStr(1, oField.Code, "REF ") = 2 Then
                If InStr(1, oField.Code, "MERGEFORMAT") <> 0 Then
                    oField.Code.Text = Replace(oField.Code.Text, "MERGEFORMAT", "CHARFORMAT")
                End If
                If InStr(1, oField.Code, "CHARFORMAT") = 0 Then
                    oField.Code.Text = oField.Code.Text + "\* CHARFORMAT "
                End If
            End If
        Next oField
    
    
    End Sub
    
  • Use this macro to format all cross references with the "Subtle Reference" style (make sure you have such a style, and that field codes are shown):

    Sub SetCrossRefStyle()
    '
    ' Macro to set styole of all cross references to "Subtle Reference"
    '
    '
        Selection.Find.ClearFormatting
        Selection.Find.Replacement.ClearFormatting
        Selection.Find.Replacement.Style = ActiveDocument.Styles( _
            "Subtle Reference")
        With Selection.Find
            .Text = "^19 REF"
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = True
            .MatchCase = False
            .MatchWholeWord = False
            .MatchKashida = False
            .MatchDiacritics = False
            .MatchAlefHamza = False
            .MatchControl = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.Find.Execute Replace:=wdReplaceAll
    End Sub
    
  • Press Alt+F9 to hide field codes

cyborg

Posted 2009-07-27T07:42:58.060

Reputation: 246

3

editing the macro uploaded by the cyborg, we can easily automate showing and hiding the field codes. so that every time we wish to update we do not have to use toggle field codes. I used following code to add field code toggle.

ActiveDocument.ActiveWindow.View.ShowFieldCodes = False

The complete macro is as follows:

Sub SetCrossRefStyle()
'
' Macro to set styole of all cross references to "Subtle Reference"
'
'
ActiveDocument.ActiveWindow.View.ShowFieldCodes = True
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Style = ActiveDocument.Styles( _
    "Subtle Reference")
With Selection.Find
    .Text = "^19 REF"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchKashida = False
    .MatchDiacritics = False
    .MatchAlefHamza = False
    .MatchControl = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
ActiveDocument.ActiveWindow.View.ShowFieldCodes = False
End Sub

This is the first time I am using macros to speed up my work in word. thanks cyborg for such helpful macro.

Jaykrushna patel

Posted 2009-07-27T07:42:58.060

Reputation: 31

0

This macro opens the Cross Reference dialogue box to insert a cross reference at the current cursor position.

When you close the Xref dialogue box after inserting the reference the macro resumes to format the inserted cross reference to superscript.

Sub XrefSuper()
'
' This opens the Cross Reference dialogue box to insert a cross reference at the current cursor position.
'   When the dialogue box is closed after inserting the reference the macro resumes to format the inserted cross reference to superscript.
'
'
Dim wc As Integer
    wc = ActiveDocument.Characters.Count

Dim dlg As Dialog
    Set dlg = Dialogs(wdDialogInsertCrossReference)
        dlg.Show 'Open dialogue and perform the insertion from the dialog box) 
                 'Macro continues after closing CrossRef dialogue box

    If wc = ActiveDocument.Characters.Count Then Exit Sub   'If we failed to insert something then exit

    Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
    Selection.Font.Superscript = wdToggle
    Selection.MoveRight Unit:=wdCharacter, Count:=1
    Selection.Font.Superscript = wdToggle
End Sub

Thanks to Graham Skan at ExpertsExchange for how to open the Xref dialogue.

TJH

Posted 2009-07-27T07:42:58.060

Reputation: 1

0

Combining answers above with another function to loop through document 'stories', to apply styling on document body, headers, footers and text on shapes.

Simply call the SetCrossRefStyle() macro below to apply "Intense Reference" style to all cross references.

Sub m_SetCHARFORMAT(textRanges As Collection)
' https://superuser.com/questions/13531/is-it-possible-to-assign-a-specific-style-to-all-cross-references-in-word-2007
'
' Set CHARFORMAT switch to all {REF} fields. Replace MERGEFORMAT.
' Requires ActiveDocument.ActiveWindow.View.ShowFieldCodes = True
'
    Dim oField As Field
    Dim oRng As Range
    For Each oRng In textRanges
        For Each oField In oRng.Fields
            If InStr(1, oField.Code, "REF ") = 2 Then
                If InStr(1, oField.Code, "MERGEFORMAT") <> 0 Then
                    oField.Code.Text = Replace(oField.Code.Text, "MERGEFORMAT", "CHARFORMAT")
                End If
                If InStr(1, oField.Code, "CHARFORMAT") = 0 Then
                    oField.Code.Text = oField.Code.Text + "\* CHARFORMAT "
                End If
            End If
        Next oField
    Next oRng
End Sub


Sub m_AddCrossRefStyle(textRanges As Collection)
' https://superuser.com/questions/13531/is-it-possible-to-assign-a-specific-style-to-all-cross-references-in-word-2007
'
' Macro to set style of all cross references to "Intense Reference"
' Requires ActiveDocument.ActiveWindow.View.ShowFieldCodes = True
'
    For Each oRng In textRanges
        oRng.Find.ClearFormatting
        oRng.Find.Replacement.ClearFormatting
        oRng.Find.Replacement.Style = ActiveDocument.Styles("Intense Reference")
        With oRng.Find
            .Text = "^19 REF"
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = True
            .MatchCase = False
            .MatchWholeWord = False
            .MatchKashida = False
            .MatchDiacritics = False
            .MatchAlefHamza = False
            .MatchControl = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        oRng.Find.Execute Replace:=wdReplaceAll
    Next oRng
End Sub


Function m_GetAllTextRanges() As Collection
' https://wordmvp.com/FAQs/Customization/ReplaceAnywhere.htm
' https://www.mrexcel.com/forum/excel-questions/443052-returning-collection-function.html
'
' Get text ranges in all document parts.
'
    Set m_GetAllTextRanges = New Collection
    For Each rngStory In ActiveDocument.StoryRanges
        'Iterate through all linked stories
        Do
            m_GetAllTextRanges.Add rngStory
            On Error Resume Next
            Select Case rngStory.StoryType
                Case 6, 7, 8, 9, 10, 11
                If rngStory.ShapeRange.Count > 0 Then
                    For Each oShp In rngStory.ShapeRange
                        If oShp.TextFrame.HasText Then
                            m_GetAllTextRanges.Add oShp.TextFrame.TextRange
                        End If
                    Next
                End If
                Case Else
                    'Do Nothing
            End Select
            On Error GoTo 0
            'Get next linked story (if any)
            Set rngStory = rngStory.NextStoryRange
        Loop Until rngStory Is Nothing
    Next
End Function

Sub SetCrossRefStyle()
'
' 1. Get all text ranges since Selection.Find only works on document body, but not on headers/footers
' 2. Add CHARFORMAT to make styling persistent
' 3. Add styling to all references
'
    Dim textRanges As Collection
    Set textRanges = m_GetAllTextRanges
    ActiveDocument.ActiveWindow.View.ShowFieldCodes = True
    m_SetCHARFORMAT textRanges
    m_AddCrossRefStyle textRanges
    ActiveDocument.ActiveWindow.View.ShowFieldCodes = False
End Sub

eymre

Posted 2009-07-27T07:42:58.060

Reputation: 36

0

Fast and effective way:

  1. Select some text with the formating you want to use for the references.
  2. Select the Home tab in the ribbon.
  3. Right-click on the Normal style and choose Update Normal to match selection.
  4. Update references with Ctrl + A, F9.

Evgeny

Posted 2009-07-27T07:42:58.060

Reputation: 1