#1
|
||||
|
||||
Macro to navigate to next CR from present location
I have Googled and found nothing. ChatGPT offered me this:
Sub JumpToNextCrossReference() Dim rng As Range Dim fld As Field ' Define the range to search for cross-references Set rng = Selection.Range ' Loop through each field in the document For Each fld In ActiveDocument.Fields If fld.Type = wdFieldRef Then ' Check if the field is within the defined range If fld.Result.InRange(rng) Then ' Select the field and exit the loop fld.Select Exit For End If End If Next fld End Sub But it is not working and I cannot understand why. SF |
#2
|
|||
|
|||
It is not working because there are no cross reference fields in the defined range.
Your code would only find and select the first wdFieldRef in the selected text. If you are on page 999 of a 1000 page document containing 25,000 cross references, does it make sense to loop through 24,900 or so to find the next one? If you want to find the next wdFieldRef from your current location then you need to define the range to be from where you are at to where you want the search to end. E.g., end of section, end of document whatever. |
#3
|
||||
|
||||
Thank you for your attention!
So if we just want to move from one CR to the next we could theoretically define the range as just the current page or section and then move on to the next one from there? Per your expert opinion, how would advise to progress? I though this would be so easy.... |
#4
|
||||
|
||||
It occurred to me that the best way to go about this is to show field codes and then simply do a search on "ref" and tweak it from there...
|
#5
|
|||
|
|||
I have already given you my opinion. In my opinion, the best way to go about this is to define a range (oRng) that starts at the end of the current selection and ends at the end of the document. Loop through the fields in that range and select the first REF field where its Result.Start is > oRng.Start.
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
VBA Command Buttons to Navigate thru Word | SnakeDoctor | Word VBA | 2 | 07-06-2023 07:47 AM |
Macro to navigate to specific point in line (Word 97) | Genericname1111 | Word | 13 | 10-06-2019 08:54 PM |
Can't navigate to entries in table of contents | treez | Word | 4 | 12-25-2018 08:37 AM |
Macro: How to get this macro to save to a specific location | LOUF | Word VBA | 1 | 12-07-2015 06:47 PM |
Macro Location | eliz.bell | Word | 0 | 10-05-2009 07:44 PM |