0
votes

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

enter image description here

1
The location in the document or the order in which they appear would be the only thing, really. Since we have no idea about the document it's not possible to offer anything more concrete. If the document is constantly being edited so that you can't use any kind of location information then you may need to bookmark the cross-references...Cindy Meister
Ah. Depending on how a cross-reference has been created, the bookmark name it references might be unique (Word-generated _REF followed by a long number.Cindy Meister
@CindyMeister, I have added some details to the question. Will bookmarking help me distinguish different cross references to same target?entryton
It may help if you can explain the purpose of the array of hyperlinks and cross references in a little more detail. It may be that you are duplicating something that Word already does for free. You may also be better off replacing your array with a scripting.dictionary of scripting.dictionaries. The key in the first dictionary would be the name of the source of the references linked to a second sub dictionary, the second sub dictionary would use the names of the the bookmarks you create for each of your cross references (e.g Ref_XZY_bm1, REF_XZY_bm2) linked to the bookmark.freeflow
@Freeflow I have added some more details in the questionentryton

1 Answers

0
votes

Thanks a lot Cindy.

Based on your comment about converting cross references to bookmarks, I updated my code as below:

sText = hl.Name + "_" + CStr(hl.range.Start)
ActiveDocument.Bookmarks.Add Name:=sText, range:=hl.range
lstCrossRef.List(lstCrossRef.ListCount - 1, 5) = sText

On selecting the item from the list, I use the below code to select the cross reference in the document.

bmName = lstCrossRef.List(lstCrossRef.ListIndex, 5)
ActiveDocument.Bookmarks(bmName).range.Select