How to replace Cross-reference whis Hyperlinks?

E

Elena Bulaeva

Hello. I have many documents contained many cross-references to
Headings. I need to replace this cross-references with Hyperlinks. How
can I do it? Is there any macros to help me. Thanks.
 
M

macropod

Hi Elena,

Any cross-reference field can act as a hyperlink, by adding the hyperlink switch (ie \h) when you create it. Alternatively, you can
add the switch later by selecting the cross-reference field and pressing Shift-F9 to expose the field code, then adding '\h' at the
right end. Press F9 to update the field.

Do note that cross-reference fields acting as hyperlinks don't look any different to the surrounding text (ie not blue & underlined
text) as in ordinary hyperlinks. If you want the cross-reference field to look like a hyperlink, format the 'R' in 'REF' or 'P' in
'PAGEREF', depening on the type of cross-reference, and add '\* Charformat' to the field code, before the '\h'.
 
E

Elena Bulaeva

Hi, macropod. Thank you for your replay.

We don't need the cross-reference field to look like a hyperlink. We
need to change cross-reference to hyperlinks, cause the method of
adding links (as hyperlinks — Insert - hyperlinks - Place In this
document) is very important for future work with this documents.

We want to find a quick method of replacing links in many documents.
Replacing links by hand will take too much time.
 
M

macropod

Hi Elena,

Since cross-references can function the same way as hyperlinks, I don't understand why you need a hyperlink, as such - especially
since inserting hyperlinks is more complicated (the process is fully explained in Word's Help file).

--
Cheers
macropod
[Microsoft MVP - Word]


Hi, macropod. Thank you for your replay.

We don't need the cross-reference field to look like a hyperlink. We
need to change cross-reference to hyperlinks, cause the method of
adding links (as hyperlinks — Insert - hyperlinks - Place In this
document) is very important for future work with this documents.

We want to find a quick method of replacing links in many documents.
Replacing links by hand will take too much time.
 
E

Elena Bulaeva

Hi, macropod,

This documents will be translated in one of the Translation memory
systems, which can work correctly with hyperlinks only. This
documents have already contaned cross-references.
 
M

macropod

Hi Elena,

I'm not sure that Word exposes any attributes from REF & PAGREF fields, etc that can be interrogated via vba and incorporated into a
HYPERLINK field. Nevertheless, you may be able to achive what you're after by embedding the cross-reference fields in hyperlink
fields. For that, you could use code like:
Sub HyperlinkXRefs()
Dim i As Integer, RngFld As Range
ActiveWindow.View.ShowFieldCodes = True
With ActiveDocument
For i = .Fields.Count To 1 Step -1
With .Fields(i)
If .Type = wdFieldRef Or .Type = wdFieldPageRef Then
.Select
With Selection
.Cut
.Fields.Add Range:=.Range, Type:=wdFieldHyperlink, Text:="\l", PreserveFormatting:=False
.MoveEnd wdWord, 1
Set RngFld = .Range
.Collapse wdCollapseEnd
.Move wdCharacter, -1
.Paste
RngFld.Collapse wdCollapseStart
End With
End If
End With
Next
.Fields.Update
End With
ActiveWindow.View.ShowFieldCodes = False
End Sub

If that doesn't achieve what you're after, I suspect you'll have to go through the document manually replacing the cross-references
with hyperlinks.

You may be concerned about my references to cross-reference fields in hyperlink fields in the above. The reason is that this is how
Word stores cross-references and hyperlinks.

Note too that, if a cross-reference field is already embedded in another field and you run the above code, you'll end up with a
hyperlinked cross-reference embedded in that field - running the code twice, for example, will produce that effect.
 
D

darksinister

Hi macropod,

I'm after almost exactly the same solution as elena and stumbled ove
your code. The problem I have is the following: I have tw
cross-references one after another and when I run your macro, i
eliminates the space between them. I tried working through your code
but i can't quite figure it out. Could you please help me on that?

best regard
 
M

macropod

Hi darksinister,

Change:
With Selection
.Cut
to
With Selection
.InsertAfter " "
.Cut
 

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