How to maintain the formatting of a Cross Reference

D

Dave

Hello All,

I have a very long document with a lot of cross references. I am using the
following macro to change the colour of my cross references to Blue.

Sub InsertCrossReference()

Dim oRange As Range
Dim oColor_Old As WdColor

Set oRange = Selection.Range
'Save current font color
oColor_Old = oRange.Characters(1).Font.Color
'Diaplay cross-reference dialog box
With Dialogs(wdDialogInsertCrossReference)
..InsertAsHyperLink = True
..Show
End With
'Include the inserted cross-reference in oRange
oRange.End = Selection.End
'Apply blue color to oRange
oRange.Font.Color = wdColorBlue
'Reset color after cross-reference
Selection.Font.Color = oColor_Old

Set oRange = Nothing

End Sub

I got it from one of the other newsgroups,it works fine as long as I don't
update my fields. The moment I update the filed the formatting reverts back
to the normal para formatting. So how do I maintain the formatting even
after I update the field.

Thanks in advance
Dave
 
G

Graham Mayor

When you update the field it re-reads the referenced item and resets it as
it was originally. To get around this you need to add a charformat switch to
the field. The dialog has no option to do this, but you can add it later eg

Sub InsertCrossReference()
Dim iFld As Integer
Dim oRange As Range
Dim oColor_Old As WdColor

Set oRange = Selection.Range
'Save current font color
oColor_Old = oRange.Characters(1).Font.Color
'Display cross-reference dialog box
With Dialogs(wdDialogInsertCrossReference)
..InsertAsHyperLink = True
..Show
End With
'Include the inserted cross-reference in oRange
oRange.End = Selection.End
'Apply blue color to oRange
oRange.Font.Color = wdColorBlue
'Reset color after cross-reference
Selection.Font.Color = oColor_Old
'add charformat switch
For iFld = oRange.Fields.Count To 1 Step -1
With oRange.Fields(iFld)
.Code.Text = .Code.Text & " \* CHARFORMAT "
.Update
End With
Next iFld
ActiveDocument.ActiveWindow.View.ShowFieldCodes = False
Set oRange = Nothing
End Sub


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
D

Dave

Thanks for the reply Graham

I tried the code you gave me and it keeps giving a syntax error for the
following lines

With oRange.Fields(iFld)
.Code.Text = .Code.Text & " \* CHARFORMAT "
.Update
End With

Any idea why?

Thanks
Dave
 
G

Graham Mayor

Did you copy the whole thing - particularly the preceding line? It works for
me in Word 2003.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
D

Dave

Thanks for that Graham, seems like I made a mistake some where. It works
fine now.

I just noticed something, the formatting for existing cross references does
not change. Do I have to manually recreate the links or is it supposed to
change existing link formats also?

Thanks
Dave
 
G

Graham Mayor

The macro has no effect on existing cross references. If you want to change
existing cross references to display in blue, you need a different macro. In
fact you can insert your references normally and use this macro to update
them all at once.

Sub ChangeFieldContent()
Dim strCodes As String
Dim iFld As Integer
Dim strFind As String
Dim strRepl As String
Selection.HomeKey
For iFld = ActiveDocument.Fields.Count To 1 Step -1
With ActiveDocument.Fields(iFld)
If .Type = wdFieldRef Then
If InStr(1, UCase(.Code), "MERGEFORMAT") <> 0 Then
.Code.Text = replace(.Code.Text, _
"\* MERGEFORMAT", "\* CHARFORMAT")
End If
If InStr(1, UCase(.Code), "CHARFORMAT") = 0 Then
.Code.Text = .Code.Text & " \* CHARFORMAT "
End If
.Code.Font.Color = wdColorBlue
.Update
End If
End With
Next iFld
End Sub

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
G

Graham Mayor

You are welcome :)

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
G

Graham Mayor

On further reflection you might usefully change

If InStr(1, UCase(.Code), "MERGEFORMAT") <> 0 Then
.Code.Text = replace(.Code.Text, _
"\* MERGEFORMAT", "\* CHARFORMAT")
End If
to
If InStr(1, UCase(.Code), "MERGEFORMAT") <> 0 Then
.Code.Text = replace(.Code.Text, _
"MERGEFORMAT", "CHARFORMAT")
End If

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
D

Dave

Thanks I will try it out and let you know how it goes. Just curious though,
but what is the difference between this marco and the one you gave
previously.

Thanks
Dave
 
G

Graham Mayor

The original replaced \*MERGEFORMAT with \*CHARFORMAT but would not account
for
\* MERGEFORMAT ie with a space. The replacement code only changes the
MERGEFORMAT part of the string.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
D

Dave

ok. Thanks again for all the help

Graham Mayor said:
The original replaced \*MERGEFORMAT with \*CHARFORMAT but would not
account for
\* MERGEFORMAT ie with a space. The replacement code only changes the
MERGEFORMAT part of the string.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top