I have a macro that fetches all the hyperlinks and cross references from the document. I add each one to a listbox that is shown to the user.
When there are multiple cross references with the same text, how can I distinguish them?
For the hyperlinks, I can use hyperlink.Name
, which will return a unique name.
What can I use to identify a cross reference? Is there a field that I can use, like Name
for hyperlink?
I use the below code to find the cross references:
For Each objFld In ActiveDocument.Fields
If objFld.Type = wdFieldRef Then
' add objFld
..
EDIT :
I am using objFld.result.Start
to get the position of the cross reference, but it goes for a toss when the user updates any preceding cross reference.
EDIT2 : I came across this answer on SO, and it prompted me to add the below to my code:
objFld.ShowCodes = True
objFld.Select
Selection.Collapse wdCollapseStart
Selection.MoveStartUntil "_"
Selection.MoveEndUntil " "
refName = Selection.Text
objFld.ShowCodes = False
So now I am able to read the field which has REF _Ref528247211 \h
and get _Ref528247211
into refName
.
However if there are multiple cross references or hyperlinks to the same target, I am not able to distinguish them.
How can I accomplish this?
EDIT2 :
I have a custom form where I list the hyperlinks and cross references. The user should be able to double click each item and update if needed. For this I need to distinguish each occurrence of hyperlink/cross reference
Below image shows my listbox populated from Section 1.11 referenced multiple times in page 39. The hyperlink names are all same
_REF
followed by a long number. – Cindy Meister